Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

fix(tui): bound both client-side queued-message resend storms - #52

Merged
yjuyjuy merged 6 commits into
masterfrom
fm/fix-queued-message-storm
Aug 28, 2026
Merged

yjuyjuy merged 6 commits into
masterfrom
fm/fix-queued-message-storm

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 27, 2026

Copy link
Copy Markdown
Owner

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_id 2..175, stopped only when the user hit Ctrl-C (session session_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 Error carrying a retry_after (or rate-limit text), the client armed rate_limit_reset, kept the pending message forever, and re-sent every reset window without ever incrementing or checking a retry counter. Bounded now with App::RATE_LIMIT_MAX_ATTEMPTS (5): each armed resend increments retry_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 of Message frames.
  • 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_terminal checks the herdr multiplexer before TERM_PROGRAM, so a leaked ambient HERDR_ENV/HERDR_PANE_ID masked the handterm signal. Cleared for the test's duration.

Full cargo test -p jcode-tui --lib green (2200 passed, 0 failed). cargo clippy -p jcode-tui --lib clean.

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_processing true 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.

firstmate crewmate 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.
@yjuyjuy
yjuyjuy merged commit 38848f5 into master Aug 28, 2026
11 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant