Repository navigation
Description
Summary
When the browser replaces its terminal buffer with a tmux capture (GET /api/sessions/:id/terminal), it has to merge that snapshot with the live output it received while the capture was downloading. It does that by arrival time alone. Live output that arrived before the response headers is treated as already in the capture and dropped; output after the headers is replayed. That boundary is wrong in both directions:
- Lost row: output produced just after
capture-paneran, but delivered to the browser before the response headers, is in neither the capture nor the replayed tail, so it never appears on screen. - Duplicated row: output already in the capture, but delivered after the headers, is replayed on top of the capture and appears twice.
tmux still holds every row, so nothing is lost there; only the browser's copy is wrong. It shows up as a missing or doubled line right where output was printing during a history load. It came up while measuring #815 (the Reload history action for #814), but it is older than that PR and affects every replay path that uses the same cutoff.
Where the cutoff lives (master 5548c17c)
src/web/public/app.js:2541,_bufferLoadFinishOpts(payload, headersReceivedAt)returns{ flushQueued: capturedFromMux, since: headersReceivedAt }.src/web/public/terminal-ui.js:5042, in_finishBufferLoad:if (entry.at < since) continue;(each queued live frame carries its client arrival time).
The JSDoc on _bufferLoadFinishOpts already describes the duplicate side: a batch pending when the capture ran (WS micro-batch WS_BATCH_INTERVAL_MS = 8, SSE 16 to 50 ms), plus output tmux had painted that the server had not yet read from the attach PTY. It doesn't cover the loss side.
Why "arrived before headers" doesn't mean "in the capture"
capture-pane is execSync, but that only blocks Node while the command runs. It doesn't stop the program or tmux, and the route keeps working after the capture returns: a separate cursor query, building the response, the API envelope, and response compression (content-encoding: zstd, transfer-encoding: chunked were observed on a 300-row capture). The session keeps emitting and broadcasting live output during that time, so a row produced after the capture can reach the browser before the reply's headers do.
Measured timeline (one lost row)
Instrumented live run: an isolated Codeman instance, tmux 3.4, headless Chromium, and a custom agent CLI running bash with altScreen: 'strip-mux-only'. A writer printed for i in $(seq 1 30); do echo during-$i; sleep 0.05; done while a manual full-history pull ran. Times are relative to capture-pane returning. Server and producer times are server-side; the browser rows are on the browser's clock, aligned by epoch time, so treat cross-machine gaps as approximate. The ordering is what matters, and both ends were on one machine:
| Step | Time |
|---|---|
capture-pane returns (no during-7 in it) |
0 ms |
Program prints during-7 |
+4.7 ms |
| Server emits it; WS broadcast | +49.9 ms / +68.4 ms |
| Browser receives and queues it (load still open) | +75.4 ms |
Server writeHead / response finished |
+87.1 ms / +93.7 ms |
Browser records headersAt |
+111.2 ms (during-7 arrived 35.8 ms before it) |
Load finishes with since = headersAt: during-7 is skipped |
+138.8 ms |
Final state: tmux holds during-7 once; the browser doesn't have it. during-8 (printed 65.5 ms after the capture) was lost the same way in that run. The same cutoff dropped a row on master's selectSession path (during-27, produced after the capture, arriving 2.0 ms before the headers).
A duplicate, in the other direction: during-2 was already in the capture, but its frame left the attach PTY after the capture and arrived 0.3 ms after headersAt, so it was replayed and shown twice.
How often
40 fixture-matched live runs (same scrollback, writer, trigger, two-tab layout, terminal size, loopback). They were not latency-equalized, so they show the cause, not reliable rates:
| Path | Build | Runs losing rows | Rows lost | Duplicate excess |
|---|---|---|---|---|
| Manual full-history pull | master | 2/10 | 3 | 0 |
| Manual full-history pull | #815 branch | 1/10 | 2 | 1 |
Tab switch (selectSession, plus the refresh that follows) |
master | 5/10 | 16 | 1 |
Tab switch (selectSession, plus the refresh that follows) |
#815 branch | 6/10 | 10 | 6 |
Not every loss in that table is this cutoff:
- Master's manual pull loses rows another way: it doesn't queue during its download, so live output goes into the old buffer and is then reset away.
- Most tab-switch losses on both builds came from the
_onSessionNeedsRefreshreplay that runs after the switch, which also fetches before opening its load. - One branch loss was an SSE to WS handoff: the row reached SSE while
wsReadywas already true, so no handler wrote it. That needs its own fix.
Possible fixes
All of these need every capture-replay path to share one contract: selectSession, the manual full-history pull and _onSessionNeedsRefresh. Fixing only one leaves the others exposed.
- Server capture watermark (strongest). A monotonic output sequence number taken at the capture boundary, returned with the terminal response and carried on WS/SSE frames, so the client replays exactly the output newer than the snapshot. It has to be aligned with the mux/attach-PTY stream, not just with when
capture-paneran: tmux can have painted bytes the server hasn't read yet. It also has to handle frames that straddle the boundary, reconnects, the SSE fallback and remote captures. - Flush or pause the broadcast around the capture. This narrows the duplicate side (the pending batch), but on its own it can't recover a row produced after the capture that arrives before the headers, and pausing doesn't stop the producer or tmux. It isn't a complete fix by itself.
- Client-side overlap matching between the capture's tail and the queued frames. It can work for plain line output, but it isn't exact for TUIs: cursor movement, erases, repaints and identical repeated lines make text matching unreliable.
Option 1 changes the wire format, so it is a separate change from #815. I'll start working on a fix once #815 (the Reload history button) is merged.
Activity
Ark0N commentedon Nov 23, 2026
Thanks for the detailed write-up and the measurements.
I checked this against master
5548c17c(1.51.1), and the code matches what you describe._bufferLoadFinishOpts(src/web/public/app.js:2541) handssince: headersReceivedAtto_finishBufferLoad, which skips every queued frame withentry.at < since(src/web/public/terminal-ui.js:5042). Nothing on the server makes "before the headers" mean "in the capture". The route is synchronous fromcapture-paneto its return, but the reply then goes through@fastify/compress(registered insrc/web/server.ts, threshold 1024 bytes), which pipes the JSON through a zlib stream. The headers go out on a later tick, and in the meantime the attach PTY is read and the 8 ms WS batch can fire. On thefull=1path the cursor query is also a separateexecSyncafter the capture. The JSDoc on_bufferLoadFinishOptsalready describes the duplicate side, and the loss side is real too. I haven't re-run your timing runs here; this is from reading the code.Two more places use the same cutoff, in case they aren't on your list:
_onSessionClearTerminal(app.js, the reload after aclearTerminalevent) also uses_bufferLoadFinishOpts, and, like_onSessionNeedsRefreshand_maybeRefetchFullHistory, it fetches before opening the load.selectSessionis the only one that calls_beginBufferLoadbefore its fetch.TerminalTile(src/web/public/terminal-tile.js, grid tiles and the split pane's Pane B) applies the same rule in its refresh and in_pullHistory. Its comment already says a frame in that window "can be lost or doubled" and is "not closable without a server-side capture time".For the wire format,
docs/versioning-policy.mdtreats new optional fields on/api/v1as additive. Two things are still open: whether Codeman takes the watermark approach (a new field on the terminal response and on the WS and SSE terminal frames, shared by all of those paths), and whether that work should wait for #815.