Closes #23661.
Problem
A host that is overloaded still answers small requests, so the socket-level heartbeat never fires, and a command
the host will never answer waits forever. The composer drops Enter while a send is in flight, silently, so the
user is left with a spinner that never resolves and no way to queue the message they just typed.
Measured on the host this was found on, against a 25.5 GB statev2.sqlite under load 68-116 on 10 cores, from
the server's own trace log over a 10-minute window:
MCP tool calls: 1,857 in ~10 minutes (peak 506 in one minute)
DB lock waits > 100 ms: 1,608 (518 seconds of lock wait in that window)
correlation calls/min vs
total lock wait/min: r = 0.751
The command itself was accepted quickly - ws.rpc.orchestration.dispatchCommand p50 5 ms, and every command
that started also ended (43/43). It was the reply path that stalled, behind a database write lock that waited
up to 6,622 ms. From another machine over Tailscale, one command got no answer for 163 seconds while
small requests to the same host answered in 6-126 ms.
Three gaps on the client side:
1. A request waits forever. packages/client-runtime/src/rpc/client.ts has no timeout, deadline, or
Effect.timeout anywhere in its 446 lines. The only bound in the transport is the socket-level ping timeout in
packages/client-runtime/src/rpc/protocol.ts:
// Pings go out every 5 seconds, and a server busy for a few seconds stops
// answering them. Allow 15 seconds without any frame before dropping the socket.
export const PING_TIMEOUT = "15 seconds";
That is a no-traffic check. A host under load keeps answering small requests, so the socket stays alive and
the one command that is stuck is invisible to it.
2. The composer drops Enter, silently. The send guard returns early when the send is busy, with no error,
no toast and no queue. sendQueuesBehindSetup only covers the worktree-setup case, not a general in-flight send.
3. Liveness cannot see a stalled command pipeline. The 15-second heartbeat answers "is the connection still
carrying frames?", not "is the host still doing the work we ask of it?".
Change
A request deadline. requestGuarded now bounds every unary RPC and fails it with a named
EnvironmentRpcTimeoutError that carries the method, the environment, the deadline in milliseconds, and a
message the UI can show. The deadline lives in packages/client-runtime/src/rpc/deadlines.ts so it is one value
in one place.
The deadline is deliberately long - 120 seconds. A legitimate request on a busy host can take minutes
(opening a long thread, starting a turn, waiting on a provider), and cutting it off early turns a slow success
into a failure the user has to retry, which is worse than waiting. It only fires when the host has genuinely
stopped answering.
The error is part of the request's failure channel (EnvironmentRpcRequestFailure), so every caller has to
handle it. That is the point: a request that cannot be answered has to be visible, not invisible.
A local composer queue. A message typed while a send is in flight is queued instead of dropped, and the
user is told it is waiting. The queue lives on the route's send state, so it survives the composer remounting
like the existing in-flight state does.
The queue carries the whole draft, not just its text, so a queued message keeps its attachments, its
terminal contexts, its review comments and its attached threads. The draft is restored before the message is
sent, so it goes out exactly like a typed one.
The queue is drained in order, one message at a time - the composer can only have one send in flight, and a
second message must not start before the first one has been accepted. A send reports whether it was
accepted: accepted only once the turn is dispatched, so a send that gave up on a blocked upload or a
missing worktree base counts as refused. A refused send puts its message back and stops the drain, so a later
message cannot overtake it, and the remaining messages go back in the order the user typed them.
A drain also leaves a queued message alone when the composer has something the user typed since: restoring the
queued draft over it would lose what they just typed, so the message stays queued and the user is told.
Liveness that notices a stalled pipeline. The session tracks the commands in flight and reports the
longest-outstanding one once it passes the stall deadline, so a command that never comes back is visible even
though the connection is technically alive. The connection is not torn down: the host may still be working
on it, and the deadline exists to say so, not to cancel work the server is about to finish.
The tracker lives in packages/client-runtime/src/rpc/commandStall.ts and is deliberately a plain object over an
injected clock, so it is testable without a clock and stays inside Effect's time model. Each command releases
its OWN entry, so two commands of the same method cannot release each other.
Scope and approval
Links #23661, filed with the measurements above and with the duplicate search the triage playbook expects.
This is one underlying problem - "a stalled command pipeline is invisible to the client" - with three parts that
only make sense together:
- The deadline alone would fail the request but still drop the user's message on the floor.
- The queue alone would keep the message but never tell the user the host is not answering.
- The stall tracker alone would report the stall but still not fail the request.
They are one client-side story about the same failure, so they are one PR. The server-side change (moving the
projection read path onto the existing SQLite reader connection) is deliberately not in this PR: it is a
different layer with a different risk, and it is filed separately as #23689.
No behaviour changes for a healthy host. A request that answers in time is untouched; a composer that is free
is untouched; the stall tracker only reports once a command has been outstanding past the deadline.
Verification
Focused tests, all red before the change and green after.
The request deadline (packages/client-runtime/src/rpc/clientTimeout.test.ts):
- a request the host never answers fails with
EnvironmentRpcTimeoutError, carrying the method, the
environment, timeoutMs: 120_000 and a message that names the method. Before the change this test hangs
until the test timeout (60 s) and fails; after, it fails in milliseconds with a name.
- the deadline fires while the clock is otherwise idle (TestClock).
- a request that answers in time still succeeds.
- a request in flight is recorded with the tracker, and the entry is released when it finishes.
- a request that times out still releases its tracker entry, so a failed request cannot leave a phantom command
outstanding.
The stall tracker (packages/client-runtime/src/rpc/commandStall.test.ts): reports nothing when no command is
outstanding; does not report a command inside the deadline; reports the longest-outstanding command; stops
reporting once the command finishes; keeps the count and the report correct when the same method is outstanding
twice; releases the command that finished rather than the oldest of the same method; and the deadline is at
least 60 s, so a slow success is not turned into a failure.
The composer queue (packages/client-runtime/src/state/composerSendQueue.test.ts): keeps a message typed
while a send is in flight; drains in the order the messages were typed; empties on drain; puts a failed message
back at the front; keeps the queued order when several unsent messages are restored; and carries a payload
the queue does not interpret.
The restore order (apps/web/src/components/chat/composerSend.logic.test.ts): the messages a drain did not
send go back in the order the user typed them, and the message the drain stopped on is restored too.
Regression runs, unchanged by this PR, on the current main:
packages/client-runtime: 121 files, 1,148 tests, all passing
apps/web: 285 files, 3,863 tests, all passing
typecheck (client-runtime, web): clean
lint: clean on every changed file
Not checked by hand: the composer behaviour was not exercised in a running app. The queue, the drain ordering,
the acceptance rule and the release-per-command are covered by the unit tests above; the wiring into the
composer is the part that would benefit from a manual pass with a deliberately slow host.