Skip to content

fix(chat): keep Stop ahead of the next message without reordering typing - #1112

Merged
devswha merged 1 commit into
mainfrom
fix/stop-intent-order
Oct 14, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/stop-intent-order

Conversation

@devswha

@devswha devswha commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Problem

Pressing Stop in the chat composer and sending a message right after can deliver the message to the agent before the Escape (#1107). The Stop was an unqueued WS input of a lone ESC, written into the PTY sidecar, where a successful write only means the bytes were queued in the pipe. The following submit goes through a herdr RPC after a guessed TYPED_SETTLE_MS (300 ms), on a separate channel, so on a loaded machine the paste can overtake the Escape. CI caught it once on main: run 38584912670 expected "\u001bafter\r" and received "after\u001b\r".

Design

  • shared/protocol.ts: the input frame gets an optional, documented stop?: true. Only the composer's Stop sets it: abortTurn in PaneTerminal.tsx marks its term.input(ESC), so the ESC still passes the existing onData guards (hidden/observe/held pane, held-draft rules, sticky-modifier chords), and HerdrSocket.sendInput adds stop: true only for that lone ESC. Older servers ignore the field and keep their current behavior.
  • server/index.ts: an attached-pane input with stop: true and text exactly ESC runs in the pane's serialized queue. It first waits out the same settle the keys path uses after the pane's last PTY keystroke (KEY_SETTLE_MS, or TYPED_SETTLE_MS after an ESC), so it never overtakes typing still in the sidecar. Then it is sent through the authorized paneSendText path, so a following submit queued behind it waits for it. After that call succeeds it is recorded like PTY typing (lastTyped + typedEscape), so submitText/dispatchPendingText still wait TYPED_SETTLE_MS and check again for a blocked prompt before pasting. The settle wait is one helper (settleAfterTyping) shared with the keys path, so server/index.ts keeps its fixed-sleep count of 8.
  • A plain input ESC (terminal typing, Vim) stays on the FIFO PTY path, unchanged. The mirrored (no PTY) branch is unchanged.
  • site/demo/transport.ts accepts the field and types the ESC like any key.

Why not #1106

#1106 routed every lone ESC through paneSendText. That let an ordinary terminal ESC overtake bytes typed just before it that were still in the sidecar (x, ESC, y delivered as ESC, y, x; in Vim i then ESC could arrive reversed). It also dropped the Stop's settle bookkeeping, which made pending-input.contract.test.ts fail (agent_blocked expected). Here only the explicit Stop changes path, it waits behind earlier typing, and it keeps the bookkeeping.

Tests

  • server/submit.contract.test.ts: the ported fix(chat): keep Stop ahead of the following message #1106 interleaving test now sends the Stop with stop: true. It holds a PTY ESC until the later RPC paste completes, waits on input-ready instead of Bun.sleep(300) (so the file's fixed-sleep baseline drops from 6 to 5), and keeps the exact "\u001bafter\r" assertion.
    • Before (this test with origin/main's server/index.ts): fails, received "after\r" instead of "\u001bafter\r".
    • After: passes.
  • New keeps a terminal ESC in order with the typing around it: holds PTY writes until all three frames were written, or until anything sent through herdr reaches the pane. Frames x, ESC, y\r must arrive as "x\u001by\r". An implementation that moves a plain ESC to send_text would deliver ESC first.
  • server/pending-input.contract.test.ts: the existing Stop settling-gap case runs unchanged for the plain ESC frame, and also runs a second time with stop: true. Its assertions are the same in both runs.
  • src/lib/ws.test.ts: only sendInput(..., true) puts stop: true on the wire.
  • scripts/ui-regression.ts: the composer's Escape-with-draft Stop asserts that the frame is { type: "input", text: ESC, stop: true }.

Commands (on isolated check sessions):

  • Before: git show origin/main:server/index.ts > server/index.ts; bun run check run sh -c 'bun test ./server/submit.contract.test.ts -t "Stop only once the Stop reached|terminal ESC in order"': exit 1. The interleaving test fails ("after\r" received, "\u001bafter\r" expected) and the FIFO test passes (plain ESC order was already correct on main).
  • After: bun run check run sh -c 'bun test ./server/submit.contract.test.ts ./server/pending-input.contract.test.ts ./server/prompt.contract.test.ts ./server/omo-prompt.contract.test.ts ./server/keys-lifecycle.contract.test.ts ./server/input-membership.contract.test.ts': exit 0, 166 pass / 0 fail.
  • FIFO guard against fix(chat): keep Stop ahead of the following message #1106's routing (scratch edit sending every lone ESC through the queue, reverted afterwards): bun run check run sh -c 'bun test ./server/submit.contract.test.ts -t "terminal ESC in order"': exit 1, received "\u001by\rx".
  • bun run check run sh -c 'bun run build && bun scripts/ui-regression.ts && bun scripts/sticky-modifiers-regression.ts': exit 0.
  • bun run typecheck: exit 0.
  • bun run test:unit: exit 0, 3612 pass / 0 fail / 8 skip.

Fixes #1107

The composer's Stop now marks its input frame with stop: true. The bridge sends that lone ESC in the pane's serialized turn through send_text, after the settle that follows the pane's last pty keystroke, and records it like pty typing so the next message still waits and checks again for a question. Plain terminal ESC keeps the pty's FIFO order. Fixes #1107.
@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: abbc9dc2-ec63-4737-a141-a65d213245d0
📥 Commits

Reviewing files that changed from the base of the PR and between c47a60a and 78db382.

📒 Files selected for processing: 11
  • changelog.d/stop-before-next-message.md
  • scripts/fixed-sleeps.json
  • scripts/ui-regression.ts
  • server/index.ts
  • server/pending-input.contract.test.ts
  • server/submit.contract.test.ts
  • shared/protocol.ts
  • site/demo/transport.ts
  • src/components/PaneTerminal.tsx
  • src/lib/ws.test.ts
  • src/lib/ws.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed message ordering after pressing Stop. A message sent immediately afterward no longer reaches the agent before the Stop action, even when the computer is busy.
    • Terminal typing now retains its order around Stop, including when Escape is typed directly rather than sent through the Stop action.

Composer Stop now marks its Escape input separately. The server serializes that input with pane text sending and applies the typing settle interval. Ordinary terminal Escape remains on the direct PTY path. Contract tests check ordering and pending-input behavior.

Changes: Stop input ordering

Layer / File(s) Summary
Mark composer Stop input
shared/protocol.ts, src/components/PaneTerminal.tsx, src/lib/ws.ts, src/lib/ws.test.ts, site/demo/transport.ts, scripts/ui-regression.ts
The input frame can carry stop: true. The composer marks its Escape input as Stop, while ordinary Escape remains unmarked. The WebSocket and demo message shapes accept the marker.
Serialize Stop and verify input order
server/index.ts, server/pending-input.contract.test.ts, server/submit.contract.test.ts, scripts/fixed-sleeps.json, changelog.d/stop-before-next-message.md
The server applies the typing settle interval before sending Stop through the serialized text path and records it as typed. Contract tests check ordering and blocked pending input. The fixed-sleep value changes from 6 to 5, and the changelog describes the ordering change.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: yoonwoo-ha, jiunshinn

Merge Risk: ⚪ Minimal · up to 78db3

No actionable merge-blocking risk is identified; the change is ready for normal merge checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 9 files. (2 skipped: 2… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: ordering Stop before the next message while preserving terminal typing order.
Description check Passed The description provides the problem, design, resulting behavior, regression rationale, validation commands, contract-test coverage, and results. It uses headings such as Problem, Design, and Tests in…
Linked Issues check Passed Issue #1107 requires ordering the composer Stop Escape before a following submit without reordering ordinary terminal Escape input. The PR marks only composer Stop with stop: true, routes that frame…
Out of Scope Changes check Passed The changes stay within issue #1107. Protocol, client, server, demo, and regression-test changes implement or verify Stop intent and input ordering. The changelog entry and fixed-sleep baseline update…
Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 9 files. (2 skipped: 2 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

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.

chat: a message sent right after Stop can reach the agent before the Escape

1 participant