A mosh diff belongs to its base, not the screen I happen to show

I fixed stale terminal rows by keeping the screen states that mosh updates actually describe, then found that character widths were part of the protocol contract too.

6 min read

On this page
  1. The screen kept pieces of the past
  2. A diff needs a starting screen
  3. When the server picks an older base
  4. Making the failure measurable
  5. Keeping the missing state
  6. The next failure was a character width
  7. What I’d tell you if you build on mosh

The screen kept pieces of the past

I build Waycar, an iPhone and iPad console for coding agents over SSH. Its live terminal uses Ghostty for rendering and can use mosh for transport. On a slow or lossy connection, I started seeing stray characters from earlier output. The connection was alive. New output arrived. Parts of the screen were simply wrong.

Ghostty drew what it was given, and mosh kept the terminal’s state. My mistake was treating the server’s terminal bytes as an ordinary stream. That breaks when an update describes a change from a screen other than the one I am showing.

Credit first: mosh’s state synchronization design is what makes this transport useful on changing networks. Waycar’s client builds on wiedymi’s swift-mosh, an independent MIT-licensed Swift implementation, vendored from v0.1.8. The failure was in how that client applied screen updates. mosh’s protocol was fine.

A diff needs a starting screen

Mosh’s State Synchronization Protocol, or SSP, keeps states in sync. It does not promise to deliver every terminal write in order. For the display, the server sends terminal operations that turn one base screen into a new screen.

You can see why that matters without knowing SSP. Say the server and client agree on a screen called A. The server sends a change from A to B. I show B, but my acknowledgement has not reached the server yet.

The server can then send a newer state C based on A, the last state it knows I acknowledged. C is newer than B, and its base is older than B.

If A and C share a cell, the diff can leave that cell out. B might have changed it. If I apply the A-to-C diff to B, that cell stays wrong. A scroll can be worse: line feeds that were right for A scroll a screen that has already moved.

Server update: base=A, new=C, operations=delta(A, C)
Wrong:         apply(operations, displayed=B)
Right:         C = apply(operations, saved[A])
               repaint(from=displayed, to=C)

This is a sketch, not the wire format. The field that matters is the base. Being newer does not tell you where the operations belong.

When the server picks an older base

A later read of the mosh-server 1.4.0 source, on 2026-11-24, found exactly two cases where it builds on the client’s last acknowledged state instead of the newest state it sent. It never picks a third base.

  1. Its oldest unacknowledged state is older than its retransmit timeout (at most 1 second) plus 100 ms.
  2. The diff from the acknowledged state is no longer than the diff from the newest state, or it is under 1,000 bytes and less than 100 bytes longer.

The timeout includes a steady uplink delay, so a steady delay only triggers the second case. Short changes, such as numbered lines, hit it on nearly every state. A long full-width row almost never does.

Making the failure measurable

On 2026-10-07, I tested against a local mosh-server 1.4.0. The workload printed 300 numbered lines. A delayed datagram endpoint held every outgoing client datagram for 150 ms, acknowledgements included. That let the server keep producing updates while its view of my screen lagged.

The runs recorded 63 to 73 older-base states each. Applying those updates directly to the newest screen left 11 to 18 of 24 rows wrong. Applying each update to its stated base left the final screen exact.

The regression test is testRealMoshServerStatesOnOlderBasesLeaveTheTerminalRight. It checks the final rows, and it checks that older-base states actually arrived. Without that second check, a quiet run could pass without ever hitting the bug.

These are measurements from that day, under a controlled uplink delay. They don’t say how often a real mobile network triggers the problem. They were enough to show that the screen could drift while the protocol kept working.

Keeping the missing state

I added MoshScreen, a small client-side terminal model, and kept a screen for each recent server state. When an update arrives, I copy its base screen and apply the resize and terminal bytes to that copy.

If the base is the screen already shown, the original operations go straight to Ghostty. If it is older, MoshScreenEncoder builds operations from the shown screen to the rebuilt one instead. Ghostty still does the drawing.

The encoder rewrites changed rows and restores terminal state such as modes, titles, cursor and text style. It can replay an upward scroll before rewriting the other differences, so scrolled-off lines reach Ghostty’s scrollback. Rows are compared exactly afterward, so when scroll detection misses, the screen still ends up right. It only costs more bytes.

This did mean writing terminal behavior in a second place. The model was written clean-room from standard terminal behavior and the bytes mosh-server was seen to send, as the package provenance records. I did not copy mosh’s terminal code into the Swift package.

The cache needs a limit too. Screens the server has marked disposable are dropped. Past 256 screens or 4 million cells, the oldest go, but never the shown screen or the oldest held one, which is the server’s fallback base.

There is a limit worth stating: if the needed base was dropped, or a restored session has no saved screens, the code passes operations through as before. The fix cannot rebuild a base it no longer has.

The next failure was a character width

The screen model fixed the wrong-base problem, then exposed another assumption. My hand-made wide-character ranges counted a text-style eye symbol as 2 columns. The Host counted it as 1.

A full-width agent status row held that symbol twice. The extra width made the model wrap where the Host did not, and every later row could drift. Repainting faithfully from a wrong model only made the mistake stick.

On 2026-10-07, I ran testRealMoshServerOlderBasesWithFullWidthSymbolRows against a local mosh-server 1.4.0 on macOS 27.0.1, again with a 150 ms delayed uplink. The old widths left 19 of 24 rows wrong, with 24 older-base states. The generated table left 0 rows wrong, with 27 older-base states. That was one run of each, not a distribution.

Later CI runs showed the second case above at work: with only a steady delay, that full-width row rarely took an older base, and some runs saw none. The test now also stalls the uplink for 1.5 seconds out of every 3, past the server’s patience, so the older bases always come.

I replaced the guessed wide ranges with a table generated from Darwin’s wcwidth_l in a recorded locale. The table records Unicode 15.0 and macOS 27.0.1. The generator has a --check mode, so a changed platform table shows up instead of slipping in.

Unicode widths are not the same everywhere. The code lists where glibc differs, and the model handles some spacing marks differently from Darwin. What matters is matching the Host’s layout. A local table and a shared screen model in the test are good evidence, not proof for every Host and renderer.

What I’d tell you if you build on mosh

Keep the base state attached to every diff all the way to rendering. Test with acknowledgements delayed, not just output delayed. Assert that the awkward protocol path happened.

Then test text that reaches the right edge. When later updates skip unchanged cells, a width disagreement changes the state you think you have.

This is one chapter of One developer, a team of AI agents, and the stack under an iPhone app, the story of how Waycar and the libraries under it were built.