This repository was archived by the owner on Sep 30, 2026. It is now read-only.
Repository navigation
fix(tui): bound both client-side queued-message resend storms - #52
Merged
Merged
Conversation
added 6 commits
August 27, 2026 19:18
A reconnect/reload race can leave a server turn "running" from the remote client's view while its history snapshot reads idle. The client dequeues its queued follow-up and sends it; the server rejects it with "Already processing a message"; the busy-recovery path (issue 1jehuang#391) puts the message back on the queue and re-adopts the running turn. When the same race clears the running-turn state on the next tick, the follow-up dispatches again, is rejected again, and re-queues again, with no backoff and no send-side de-dup. Observed live: one queued message sent to the server 174 times over ~12 minutes, stopped only by the user hitting Ctrl-C (session_crab_1787339035764_29f00144e5a5d1d6). Bound busy-rejection recoveries of the same submission (keyed on its stable idempotency nonce) at App::BUSY_RECOVERY_MAX_ATTEMPTS. Below the cap the behavior is unchanged, so the ordinary one-or-two-cycle race still recovers cleanly. At the cap the recovery stops re-queuing, drops the pending message, restores the user's text to the input box (never silently dropped, per issue 1jehuang#391), tells the user, and settles the client into an idle state so the next tick cannot re-dispatch. The counter resets on turn completion, session change, and /cancel. Adds a deterministic regression test that drives the full dispatch -> busy-reject -> re-queue loop far past the cap and asserts the message is sent a bounded number of times, the loop terminates, the queue is drained, and the message lands back in the input box. It also counts the Message frames that actually reach the socket to prove the wire-level storm is bounded.
detected_resume_terminal checks the herdr multiplexer's HERDR_ENV/HERDR_PANE_ID before TERM_PROGRAM. When the suite runs inside a herdr pane those variables leak into the process, so the test's TERM_PROGRAM=handterm was masked and detection returned "herdr", failing the assertion. Clear the higher-priority herdr variables for the duration of the test (restored on drop) so it asserts the handterm signal it intends. Adds an EnvVarGuard::unset helper for scoped removal.
The bounded busy-rejection fix and its regression test grow three tracked production files (app.rs, remote/server_events.rs, tui_lifecycle.rs) and two tracked test files (remote_tests.rs, remote_events_reload_04.rs) that were already over the 1200-LOC ratchet threshold. Record their new sizes; the additions are the minimal cap constant, state field, match arm, and the deterministic regression tests.
Apply cargo fmt to the queued-message storm fix so the Format and Quality Guardrails checks pass. Pure line-wrap and trailing-blank-line normalization in the busy-rejection recovery path; no behavior change. - crates/jcode-tui/src/tui/app/remote/queue_recovery.rs - crates/jcode-tui/src/tui/app/remote/server_events.rs - crates/jcode-tui/src/tui/app/remote_tests.rs
The busy-recovery baseline bump measured remote_tests.rs before rustfmt ran, recording 1387 LOC. The canonical formatted file is 1388 LOC, so the oversized-test ratchet (scripts/check_test_size_budget.py) failed once the formatting fix unblocked it in CI. This failure was previously hidden because the Format step failed first and skipped the ratchet steps. Record the true post-format size. Matches scripts/check_test_size_budget.py --update exactly; no other tracked file changed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two client-side resend engines in the remote (TUI-over-daemon) path shared the same disease: an unbounded client re-send with no backoff and no send-side de-dup. Either could storm a server that stays busy or rate-limited. This PR bounds both.
1. Busy-rejection queued-message re-loop (
fix(tui): bound the busy-rejection queued-message resend loop)Root cause: a reconnect/reload race can leave a server turn "running" from the remote client's view while its history snapshot reads idle. The client dequeues its queued follow-up and sends it; the server rejects it with
Already processing a message; the busy-recovery path (issue 1jehuang#391) re-queues the message and re-adopts the running turn. When the same race clears the running-turn state on the next tick, the follow-up dispatches again, is rejected again, and re-queues again, with no backoff and no send-side de-dup.Observed live: one queued message (
stop at the next stopping point) sent to the server 174 times over ~12 minutes,request_id2..175, stopped only when the user hit Ctrl-C (sessionsession_crab_1787339035764_29f00144e5a5d1d6). The server's per-connection nonce dedup hid it from the transcript, so the storm was visible only in the logs.Fix: bound busy-rejection recoveries of the same submission (keyed on its stable idempotency nonce) at
App::BUSY_RECOVERY_MAX_ATTEMPTS. Below the cap the behavior is unchanged, so the ordinary one-or-two-cycle race still recovers cleanly. At the cap the recovery stops re-queuing, drops the pending message, restores the user's text to the input box (never silently dropped, per issue 1jehuang#391), tells the user, and settles the client idle so the next tick cannot re-dispatch. The counter resets on turn completion, session change, and/cancel.2. Rate-limit-reset resend loop (cherry-picked WIP from
fm/scout-jcode-serverbusy-resend)The sibling engine: on any server
Errorcarrying aretry_after(or rate-limit text), the client armedrate_limit_reset, kept the pending message forever, and re-sent every reset window without ever incrementing or checking a retry counter. Bounded now withApp::RATE_LIMIT_MAX_ATTEMPTS(5): each armed resend incrementsretry_attempts; at the cap it stops re-arming, drops the pending message, and falls through to the terminal error path (surfaces the error, offers a fallback route).Tests
busy_rejection_reloop_resends_queued_message_is_bounded(new): drives the full dispatch -> busy-reject -> re-queue loop far past the cap and asserts the message is sent a bounded number of times, the loop terminates, the queue is drained, the message lands back in the input box, and the socket carries a bounded number ofMessageframes.test_remote_rate_limit_resend_is_bounded_and_stops(from the scout WIP): asserts the rate-limit resend increments and terminates at the cap.test(tui): fix handterm terminal-detection test under herdr: a pre-existing test-isolation bug unrelated to this change;detected_resume_terminalchecks the herdr multiplexer beforeTERM_PROGRAM, so a leaked ambientHERDR_ENV/HERDR_PANE_IDmasked the handterm signal. Cleared for the test's duration.Full
cargo test -p jcode-tui --libgreen (2200 passed, 0 failed).cargo clippy -p jcode-tui --libclean.Test coverage declaration
Not built strictly test-first (the fix reused the scout's diagnosis and a green scaffold), but both loops now have deterministic unit-level regression coverage that fails on the unbounded behavior and passes on the bounded behavior. No live end-to-end (organic reconnect-race) coverage: the race that opens the door is timing-dependent and needs a server test hook to hold
client_is_processingtrue across a simulated reconnect; the diagnosis handoff judged that not worth the hours, and the in-process loop (the amplifier that actually storms) is fully covered.Links both root causes: the busy-rejection handoff and the scout rate-limit-reset report.