Skip to content

test(qwen3.5-MTP): de-flake mid-cycle-cancel heal e2e + deterministic heal coverage - #84

Merged
Brooooooklyn merged 1 commit into
mainfrom
fix/cancel-midcycle-mtp-flake
Jul 4, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
fix/cancel-midcycle-mtp-flake

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

What & why

cancel_midcycle_then_continue_mtp_matches_ar was the only failure on main
CI for #83 (run 28691184880, e2e qwen3_5-dense). The
byte-identical PR-head tree (e2190e36, tree 6761d493) passed the same job
on #83's own CI — a flaky oracle, not a product regression. The RoPE/perf changes
in #83 only shifted the flake odds.

Two root causes, both found by running the code rather than reasoning:

  1. Racy committed count. The async handle.cancel() commits a
    host-timing-dependent number of turn-1 tokens (the streaming emit loop pushes
    each token to cached_token_history before its cancel check, and a
    "cancelled" stop — unlike "length" — does not force the trailing-token
    drop in engine/cache.rs). The old oracle guessed n_mtp. Worse, the
    committed count isn't observable from the stream: ChatStreamChunk.prompt_tokens
    on the streaming delta path is hardcoded to the delta length for both the
    warm and the desync-heal arms, and cached_tokens is the full prior history
    either way — neither distinguishes them.

  2. AR is the wrong oracle. MTP-speculative and plain-AR pick different tokens
    on T=0 argmax near-ties, so an AR reference diverges from the MTP run regardless
    of the heal (a sweep showed byte mismatches at longer budgets even with matched
    committed length).

The fix

Flakiness — read the real state instead of guessing:

  • New read-only #[doc(hidden)] hook mtp_flat_state_for_test() -> (committed_len, flat_mtp_caches_desynced, full_reprefill_count) (not #[napi]).
  • cancel_midcycle_then_continue_mtp_matches_uncancelled (renamed): reads the
    actual committed history and length-matches an un-cancelled MTP reference's
    budget (b* = n_mtp + (h1_mtp − h1_ref0)) so both runs commit the identical
    greedy prefix, then asserts the follow-ups are byte-identical. Same generation
    path on both sides ⇒ equal committed length is an identical prefix.

Non-vacuous heal coverage — the cancel test is still vacuous on a fast host
(the cancel always lands on a clean boundary → turn 2 warm-continues), so add
desync_heal_reprefills_to_uncancelled:

  • The flat streaming MTP continue heal is deterministically reachable — on a flat
    (non-paged) model the rebuild gate in chat_stream_tokens_delta_sync_inner
    reduces to flat_mtp_caches_desynced alone. New force_flat_mtp_desync_for_test()
    arms it. The heal re-prefills from cached_token_history and discards the caches,
    so arming the flag on a clean session faithfully exercises it.
  • New monotonic flat_full_reprefill_count (incremented only in the streaming-delta
    rebuild branch) is the only observable proof the rebuild ran. The test forces
    the desync, asserts the counter incremented (heal actually took the re-prefill
    path), and asserts the healed reply equals a byte-identical un-cancelled MTP
    reference over the same committed history.

Testing

Verified on M5 (Qwen3.5-0.8B, fresh mlx convert -m qwen3_5):

  • cancel_midcycle_then_continue_mtp_matches_uncancelled — 5/5 stable
  • desync_heal_reprefills_to_uncancelled — 5/5 stable (healed=true each run)
  • full qwen3_5_delta_chat file — 9/9
  • mlx-core lib — 1930 passed; 0 failed (46 ignored)
  • clippy -D warnings + cargo fmt --check clean
  • No NAPI surface change (accessors aren't #[napi]), so no index.d.cts regen

Two Codex adversarial-review rounds: round 1 needs-attention (test could pass
without exercising the heal) → round 2 approve after the deterministic heal
test + counter.

model-e2e label added so the e2e qwen3_5-dense job runs the fixed test on this PR.

🤖 Generated with Claude Code


Note

Low Risk
Changes are #[doc(hidden)] test hooks and e2e test logic only; no public NAPI surface and no alteration of heal behavior beyond counting reprefills for observability.

Overview
Adds test-only introspection for flat MTP cache healing: a monotonic flat_full_reprefill_count (incremented when the streaming delta path discards caches and re-prefills), plus #[doc(hidden)] model-thread commands and async helpers mtp_flat_state_for_test() and force_flat_mtp_desync_for_test() to read committed history length, desync flag, and heal count, or to arm the heal deterministically.

cancel_midcycle_then_continue_mtp_matches_uncancelled replaces the AR oracle with an un-cancelled MTP reference whose turn-1 budget is adjusted from the real cached_token_history length (fixing flaky guesses about cancel vs length stop). desync_heal_reprefills_to_uncancelled is new: forces desync, asserts the reprefill counter increased, and checks turn-2 text matches a warm MTP run over the same history.

Reviewed by Cursor Bugbot for commit f4c4b21. Bugbot is set up for automated code reviews on this repo. Configure here.

…stic heal coverage

`cancel_midcycle_then_continue_mtp_matches_ar` was the sole failure on main
CI for #83 (run 28691184880, `e2e qwen3_5-dense`); the byte-identical PR-head
tree passed the same job — a flaky oracle, not a product regression.

Two root causes (both found by running the code, not reasoning):

1. Racy committed count. The async `cancel()` commits a host-timing-dependent
   number of turn-1 tokens (the streaming loop pushes to `cached_token_history`
   before its cancel check, and a "cancelled" stop — unlike "length" — does not
   force the trailing-token drop). The old oracle guessed `n_mtp`. The committed
   count is not readable from the stream: `ChatStreamChunk.prompt_tokens` on the
   streaming delta path is hardcoded to the delta length for both the warm and
   the desync-heal arms, and `cached_tokens` is the full prior history either
   way, so neither distinguishes them.

2. AR is the wrong oracle. MTP-speculative and plain-AR pick different tokens on
   T=0 argmax near-ties, so an AR reference diverges from MTP regardless of the
   heal.

Fix:
- Add a read-only test hook `mtp_flat_state_for_test() -> (committed_len,
  flat_mtp_caches_desynced, full_reprefill_count)` so a test can read the ACTUAL
  committed history length path-independently. `#[doc(hidden)]`, not `#[napi]`.
- Rewrite the cancel test (renamed `..._matches_uncancelled`): read the real
  committed history and length-match an UN-CANCELLED MTP reference's budget
  (b* = n_mtp + (h1_mtp − h1_ref0)) so both runs commit the identical greedy
  prefix; assert the follow-ups are byte-identical. Deterministic.

The cancel test is still vacuous on a fast host (the async cancel always lands
on a clean boundary, so turn 2 warm-continues), so add a deterministic companion
`desync_heal_reprefills_to_uncancelled`:
- The flat streaming MTP continue heal is deterministically reachable: on a flat
  (non-paged) model the rebuild gate in `chat_stream_tokens_delta_sync_inner`
  reduces to `flat_mtp_caches_desynced` alone. Add `force_flat_mtp_desync_for_test()`
  to arm it; the heal re-prefills from `cached_token_history` and discards the
  caches, so arming the flag on a clean session faithfully exercises it.
- Add a monotonic `flat_full_reprefill_count`, incremented only in the
  streaming-delta rebuild branch, as the only observable proof the rebuild ran.
  The test forces the desync, asserts the counter incremented (heal actually
  ran), and asserts the healed reply equals a byte-identical un-cancelled MTP
  reference over the same committed history.

Verified (M5, Qwen3.5-0.8B fresh `mlx convert -m qwen3_5`): both new tests 5/5
stable, full `qwen3_5_delta_chat` file 9/9, `mlx-core` lib 1930/0 (46 ignored),
clippy `-D warnings` + fmt clean. Two codex adversarial-review rounds
(needs-attention → approve).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cursor

cursor Bot commented Jul 4, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_7ff8154d-23dc-4aa7-9b24-98397558865c)

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 94e985b4-2b52-472d-9990-ae221c28b53c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/cancel-midcycle-mtp-flake

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Brooooooklyn Brooooooklyn added the model-e2e Run the heavy Model E2E workflow (per-family real-checkpoint tests) on this PR label Jul 4, 2026
@Brooooooklyn
Brooooooklyn enabled auto-merge (squash) July 4, 2026 16:01
@Brooooooklyn
Brooooooklyn merged commit c1bb68a into main Jul 4, 2026
15 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/cancel-midcycle-mtp-flake branch July 4, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model-e2e Run the heavy Model E2E workflow (per-family real-checkpoint tests) on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant