szn

Bug 520 — duplication cluster 4

520. Four duplication sites with drifted or drifting copies

Severity: LOW (code quality) Status: ✅ FIXED Files: src/server/dispatch.zig:140-181, src/server/server.zig:2714-2753, src/layout.zig:295-303, src/mode_copy.zig:769-842, src/cmd/cmd.zig

Sites

# Duplication Locations
1 Non-blocking write loop: write → retry INTR → on EAGAIN stash the remainder and arm POLLOUT → advance offset dispatch.zig:140-181 (sendResponse), server.zig:2714-2753 (sendRequestCellSize), server.zig:2884-2943 (writeFrameToClient), server.zig:2949-2992 (flushDisplayClient), pty.zig:291-346 (writeInput/flushInput)
2 Byte-identical function bodies layout.zig:295-303: removePane and extractPane are the same three lines under two names
3 Mirrored keymaps: arrows, page up/down, home/end mode_copy.zig:769-816 (handleViKey) vs :822-923 (handleEmacsKey)
4 Three-line active-target prologue ~30 sites in cmd.zig, e.g. cmdSendKeys:150-152, cmdSplitWindow:161-163, cmdSelectPane:303-304, cmdKillPane:312-314, cmdSwapPane:321-323, cmdPasteBuffer:511-513

Why site 1 is the one that matters

The five write loops are not merely redundant — one has already drifted. sendRequestCellSize writes only a 5-byte header inline, does not break after the first EAGAIN when out_buf was already non-empty, and silently ignores non-EAGAIN write errors. Bugs #262 (“protocol message corruption on partial write in sendRequestCellSize”), #403 (“busy-spins forever on EAGAIN”) and #472 (“dispatch write loops slice with @intCast(n) without clamping”) are all defects that existed because the loop was copy-pasted rather than shared.

Site 2 invites the two call sites to drift, as Window.removePane / Window.extractPane already have (they differ only in the empty-list branch and whether resize runs).

Proposed Fix

  1. Extract one writeOrQueue(fd, data, *out_buf, *events) helper (plus one MAX_OUT_BUF cap helper) and route all five sites through it.
  2. Delete Layout.extractPane; alias it to removePane.
  3. Factor the shared movement dispatch into applyMovement(k, grid) bool that both copy-mode keymaps call.
  4. Add one activeTarget(server) ?Target helper returning { session, window, pane }.

Note on overlap

Bug #477 was “duplication and dead-code cluster 3” and covered triplicated split math (splitSizes call sites) and mirrored resize handlers. Site 1’s write loops, site 2 and site 4 are not covered there and should be reconciled against it when #477’s fix is re-read.

Fix

Deliberate deviation from the Proposed Fix: site 1 was not collapsed into a single writeOrQueue(fd, data, *out_buf, *events) helper. The five callers have genuinely different buffering policies that are load-bearing:

So the fix centralizes only the truly shared fragment — the raw write(2) + errno + EINTR/EAGAIN dispatch — into a new src/io.zig:

pub const WriteOutcome = union(enum) { wrote: usize, would_block, closed, failed };
pub fn writeOnce(fd: i32, buf: []const u8) WriteOutcome { ... }

Each caller switches on WriteOutcome, preserving its own buffering policy:

Site File Policy preserved
sendResponse (header+body) src/server/dispatch.zig inline usleep retry on .would_block; .closed→ConnectionClosed, .failed→WriteFailed
sendRequestCellSize src/server/server.zig non-blocking stash-on-partial / EAGAIN, but .closed falls through to the original queue-whole-slice path
writeFrameToClient src/server/server.zig .would_block/.closed→break, .failed→clear+removeFdEvents+return true
flushDisplayClient src/server/server.zig same mapping as writeFrameToClient
writeInput / flushInput src/server/pty.zig .would_block/.closed→break, .failed→WriteFailed/clear+return true

Regression tests

No new test cases were added for #520 itself — the cluster is pure refactor (behaviour-preserving), and the full suite (1058 tests) passes on both Zig 0.16.0 and 0.17.0 after the change, which is the regression gate. The moveToLineEnd arity fix is exercised by the existing copy-mode movement tests.

Verification