croft-software 0.2.232

VSCode-style TUI written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# croft web: a browser client for the session host

Design RFC, now built. Shipped:

- The WebSocket transport (#341).
- Its token, TLS and listener lifecycle (#343).
- The page (#342).

What shipped is described at the end, under [The transport](#the-transport),
[The page](#the-page) and [Threat model](#threat-model).

The session host already:

- fans one inner croft out to N clients;
- enforces write control server-side;
- sizes everyone to the smallest window;
- survives detach.

A browser is another client. What follows is what that costs.

## The decision, up front

**Ship the byte stream (option a), with a negotiated image sidechannel.**

The cell-diff protocol (option b) is the more elegant idea, but the wrong
first move. The reason only shows up once you look at where images are
written.

## What crosses the wire

### Option (a): the existing byte stream

The host relays raw PTY bytes today, and the wire format is already there:

```
[type: u8][len: u32 be][payload]      session_host.rs:14-17
FRAME_BYTES = 0, FRAME_CONTROL = 1     session_host.rs:31-33
```

- `encode_frame` is a 5-byte header plus the payload (`session_host.rs:140-158`).
- `FrameReader` (`:160-196`) resynchronises on framing.
- It skips a malformed control payload rather than poisoning the stream.
- The load-bearing part: it **ignores unknown frame types**:

```rust
// Unknown frame type from a newer peer: ignore the payload.
_ => {}
```

So a new frame type can be added without breaking existing hosts or clients.
`Claim`, `HostSwap` and `ServerHello` already rely on that tolerance.

**The cost:** the browser needs a terminal emulator in JS. A vendored
emulator is a second implementation of something croft already has, and it
will disagree with the Rust one at the edges.

### Option (b): ship changed cells

The host runs croft's own emulator and sends a cell grid. The page becomes a
dumb painter: no JS emulator to vendor, and rendering matches the terminal by
construction.

**The headless half is already solved**, better than the issue assumed:

- `PtyTerminal` owns `Arc<FairMutex<Term<VoidListener>>>` (`terminal.rs:442`).
- `VoidListener` (`:163-199`) handles only `Bell`, `PtyWrite` and
  `TextAreaSizeRequest`, answering the last with `cell_width: 1, cell_height: 1`.
- No display, no window handle, no pixel geometry.
- Bytes are fed with a plain `Processor::advance` call (`:1762`).
- `grid_lines_ansi` (`:1404-1461`) already exports the grid as text plus SGR,
  touches no ratatui type, and has seven non-render callers.
- ratatui appears 9 times in a 9242-line file, all inside `impl Widget`.

So option (b) is not blocked on the emulator. It is blocked on four things:

1. **No damage tracking exists.**
   - The only dirty signal is one whole-terminal `AtomicBool`
     (`terminal.rs:443-445`).
   - alacritty's own `Term::damage()` is not used anywhere.
   - Option (b) would need to keep a previous-frame grid and diff it: new
     code, on the hot path.
2. **`PtyTerminal` bundles the PTY.**
   - `spawn_with_preamble` always opens a pty and spawns a child (`:1714+`).
   - No constructor takes an existing byte stream.
   - A headless owner needs a new entry point, or `Term::new` +
     `Processor::advance` directly, as the tests do.
3. **A cell is bigger than it looks.**
   - `alacritty_terminal 0.26`'s `Cell` is
     `{c: char, fg: Color, bg: Color, flags: Flags, extra: Option<Arc<CellExtra>>}`
     (`cell.rs:131-140`).
   - `flags` is a 17-bit bitfield.
   - `extra` carries combining marks, per-cell underline colour, and OSC 8
     hyperlinks, which croft uses.
   - Naively 8-12 bytes per cell before `extra`.
4. **It cannot see the images.** This is the one that decides it.

### Why images decide it

Inline images never enter the ratatui buffer or any client's alacritty grid.
They are written straight to stdout, after the frame, with absolute cursor
positioning (`App::flush_activity_image_overlays` in `app/mod.rs`, and the
overlays in `app/inline_images.rs`):

```rust
let _ = write!(out, "\x1b[?25l\x1b[s");
for ((x, y), seq) in &overlays {
    let _ = write!(out, "\x1b[{};{}H", y + 1, x + 1);
    let _ = out.write_all(seq.as_bytes());
}
let _ = write!(out, "\x1b[u");
```

- **On the byte path they pass through verbatim.** That is the host's promise
  (`session_host.rs:5-7`: byte-transparent, "Kitty graphics and every escape
  sequence pass through untouched").
- **On a cell-grid path they would be invisible.** The grid has nothing where
  the picture is.
- So option (b) needs an image sidechannel anyway. It does not avoid the
  second protocol; it adds one on top of losing byte-transparency.
- Option (a) needs the sidechannel too, but only to *improve* a path that
  already works, not to repair one that does not.

### Measurement, outstanding

The issue asks for measured byte rates for (a) and (b) on a scrolling
`cargo build`. **Neither is measured, and no number should be inferred from
this document.** There is no benchmark, no frame-size counter, and for (b) no
diffing code to measure.

What the tree does record:

- `pty_pending_bytes` (`terminal.rs:446-455`) exists to distinguish
  interactive echo from a bulk stream, so that "large ones stay capped so they
  can't saturate the ssh pipe and starve input."
- `is_remote_session()` (`app/mod.rs`) throttles PTY redraws further
  over SSH for the same reason.

Byte-rate saturation over a network hop is a known, already-mitigated problem
here. That argues for measuring before committing to (b), not for assuming
(a) is cheap.

## Fonts and glyphs

- Explorer icons and the activity bar are Private Use Area Nerd Font glyphs.
- A viewer must not have to install anything, so the page embeds a
  **subset woff2** of the glyphs croft actually uses.
- The set is enumerable: it is what the icon tables reference, not an
  open-ended range.

## Input

**Keyboard is where a browser client loses the most, and the loss is
structural.**

- croft receives Cmd chords at all only because it installs forwarders into
  the terminal emulator.
- `Chord::iterm2_forwarder` (`keymap.rs:82-116`) and `ghostty_trigger`
  (`:120-145`) re-emit them as CSI-u.
- `iterm2.rs` relocates conflicting menu items.
- A browser has no equivalent escape hatch: `preventDefault()` cannot reach a
  chord the OS or the browser claims at window level.

Unreachable, with what croft binds them to:

| Chord | Browser takes it for | croft uses it for |
| --- | --- | --- |
| `Cmd/Ctrl+W` | Close tab | Close the active editor tab; close the terminal |
| `Cmd/Ctrl+T` | New tab | Open another terminal |
| `Cmd+Q` | Quit (macOS) | — (`Ctrl+Q` is Quit) |
| `Cmd+Shift+W` | Close window | `Cmd+K Shift+W`, Reopen Closed Editor |
| `Cmd+Shift+T` | Reopen closed tab | Open a terminal; restore a closed pane |
| `Cmd+Shift+R` | Hard reload | Jump to Remote (SSH) |
| `Cmd+0` / `Cmd+8` / `Cmd+9` | Zoom reset, jump to tab N | Bound |

Contested but generally preventable:

- `Cmd+P` (Quick Open, croft's most-used chord)
- `Cmd+Shift+P` (Command Palette; Firefox takes it for a private window)
- `Cmd+S`, `Cmd+F`
- `Cmd+L` (unpreventable in Firefox)
- `Cmd+Shift+D`, `Cmd+Shift+E`, `Cmd+Shift+B`

**The mitigation is already in the design.**

- `Cmd+K` is croft's chord prefix, with 55 bindings under it.
- Everything after the prefix is a *second* keystroke inside croft's own
  state machine, so the entire `Cmd+K` family survives intact.
- The Command Palette lists every named command with its keybinding.
- So the unreachable set is single-keystroke chords whose function is
  reachable another way.

The RFC's position: **do not remap anything for the web client.** Document the
unreachable set, and let `Cmd+K` and the palette carry it.

The browser must also reproduce, matching the terminal path:

- croft's VT translation for unbound keys: arrows, most `Ctrl+letter`,
  `Alt+x`, function keys (`KEYBINDINGS.md:507`), currently done by croft's
  crossterm router;
- SGR mouse encoding.

## Images

- On the iTerm2 and Kitty paths the payload is **base64-encoded PNG**
  (`iterm2_inline.rs:738-750`, `:762-804`).
- A browser can drop it into a `data:image/png;base64,...` URL with no
  transcoding.
- Kitty needs de-chunking across `m=1` continuations; iTerm2 is one blob.
- **Sixel is different**: NeuQuant-quantised and DCS-wrapped (`:828-947`). A
  web gateway should negotiate iTerm2 or Kitty and never Sixel.

Sizing is in **cells**, not pixels (`width={width_cells};height={height_cells}`),
so the client needs croft's cell metrics to place a picture. A browser can
draw these natively, which plausibly makes it croft's best image host. That is
an outcome, not a reason to choose a protocol.

## Trust boundary

**This would be the first listening socket in croft's history.** That is
measured, not rhetorical:

- Every `TcpListener::bind` in the tree today is either a test or a
  bind-to-probe-a-free-port (`remote.rs:1565-1569`, `dap/transport.rs:342`).
- `tokio::net` appears zero times; `tokio`'s `net` feature is deliberately
  absent.
- No HTTP server or websocket crate is in `Cargo.lock`, not even
  transitively.
- What exists is Unix sockets at mode 0600 (`session.rs:464`) and SSH.

So the current boundary is **filesystem permissions plus SSH**. The host says
so itself (`session_host.rs:836-839`): the random token "is a discriminator
(inner croft vs regular participant), not the trust boundary; account
possession remains that."

A browser client replaces that with a network boundary. In scope for the
follow-ups (#341, #342, #343), to be signed off *before* any listener lands:

- **LAN exposure.** Default bind must be loopback. A LAN bind is an explicit,
  separate opt-in, never a convenience default.
- **Token leakage via URL.** A token in a query string lands in history, in
  `Referer`, and in any logging proxy. Prefer a one-time exchange for a
  cookie or header credential.
- **CSRF against a local listener.** A page on any origin can issue requests
  to `localhost`. Requires origin checking and a non-cookie-only credential.
- **The write-control model must not weaken.** Server-side enforcement
  (`session_host.rs:1354-1370`) makes read-only real rather than advisory. A
  web client is another client under the same rule, with no bypass for
  convenience.

**A browser client joining at a different size shrinks the PTY for
everyone:**

- `min_winsize` (`:207-212`) takes the minimum across clients.
- A phone-sized viewport would reflow the session for every attached terminal.
- Observer (zero size, already filtered out) or full participant is a product
  decision this RFC does not make.

## Phased plan

1. **A gateway process, no listener.** Re-implement the client pump against
   the Unix socket, exercised by a test harness rather than a browser.
   `attach_client` cannot be reused: it enables raw mode and sizes from
   `crossterm::terminal::size()` (`session_host.rs:1731-1738`, `:1783`).
2. **Loopback listener plus token** (#343). The trust boundary decisions above
   must be signed off first.
3. **The page** (#342): grid painter, subset woff2, VT translation, SGR mouse.
4. **Image sidechannel.** A new frame type, using the unknown-type tolerance so
   older clients ignore it.
5. **Measure.** Only with a working (a) implementation is a real comparison
   against (b) possible.

## What this RFC does not settle

- The byte-rate comparison the issue asks for. Unmeasured, deliberately not
  estimated.
- Whether a web client is an observer or a full participant in `min_winsize`.
- IME, which needs a browser-side prototype rather than a reading of this tree.
- Per-browser preventability of the contested chords above. That is platform
  behaviour, to be verified per browser rather than asserted here.

## The transport

`croft web [WORKSPACE] [--bind ADDR]` serves the running session of a
workspace (the one `croft attach` started) over WebSocket.

It is a bridge, not a second host:

- Each WebSocket connection opens its own connection to the session host's
  Unix socket, so a browser tab is an ordinary participant.
- It has its own output queue.
- It attaches read-only unless it is first or is granted control in
  `Session: Participants`.
- It leaves the roster when the tab closes, exactly as a terminal client that
  detaches.

### Framing

**Framing.** RFC 6455 over a hand-rolled HTTP/1.1 upgrade (`src/web/ws.rs`),
no framework and no new crates. The host's frames map one to one:

| Session host frame | WebSocket message |
|---|---|
| `Frame::Bytes` (PTY output, keystrokes) | binary |
| `Frame::Control` (`hello`, `resize`, `presence`, ...) | text, the same `{"t": ...}` JSON |

- A client opens with a text `{"t":"hello","name":...,"cols":...,"rows":...}`.
- It then sends keystrokes as binary messages and `resize` as text.
- A text message the host would not accept is dropped, as the host drops
  malformed control frames.
- Client frames must be masked. These close the connection: an unmasked
  frame, a message over 1 MiB, or non-UTF-8 text.

### Connecting

**Connecting.** Every connection presents the per-run token printed at
start-up, as a WebSocket subprotocol:

- The client offers two: `croft` and `croft.token.<token>`.
- The server selects `croft`.

From a browser:

```js
new WebSocket("ws://127.0.0.1:7681/", ["croft", "croft.token." + token]);
```

A browser cannot set headers on a WebSocket, but subprotocols travel in the
`Sec-WebSocket-Protocol` header, so the token never sits in a URL. The reply
names only `croft`, never the token.

### Options

**Options.**

| | |
|---|---|
| `--bind ADDR` | Listen on `ADDR` instead of `127.0.0.1:7681`. A non-loopback address is served, with a stderr warning (see below). |
| `--tls CERT KEY` | Serve TLS (`wss://`) with a PEM certificate chain and private key. croft does not generate certificates. |
| `--rotate-token` | Stop this workspace's listener and start one with a new token, on the same address unless `--bind` says otherwise. |
| `--off` | Stop this workspace's listener. The session keeps running. |

- The `--bind` warning names the exposure.
- Without `--tls`, it also says the token and the screen cross the network in
  plain text.

### Lifecycle

**Lifecycle.**

- A listener serves one workspace's session and exits when that session ends.
- It records its address beside the session socket (`<hash>.mux.sock.web`,
  mode 0600, never the token).
- So `croft ls` shows `web: ws://...` for an exposed session, and `--off` and
  `--rotate-token` find the listener to stop.
- A second `croft web` for the same workspace is refused while one is running.
- A record whose listener was killed is cleared the next time it is read.

### Remote access

**Remote access.** To reach a session from another machine, leave `croft web`
on loopback and put something in front of it:

- Tailscale Serve, which terminates TLS with a real certificate and only
  answers your tailnet: `tailscale serve --bg 7681`.
- Caddy as a reverse proxy: `reverse_proxy 127.0.0.1:7681` in a site block.
- A certificate from `mkcert` for `--tls`, when the browser is on the same
  machine or you install mkcert's root on the other.

## The page

`croft web` prints `http://127.0.0.1:7681/#token=...`. Opening it loads a page
the listener serves from the binary:

- Files: `/`, `/term.js`, `/app.js` and `/icons.woff2`, about 65 KB together.
- A Content-Security-Policy allows the page's own scripts, `data:` images and
  a WebSocket back to the host it came from, and nothing else.
- The token sits in the fragment, which the browser never sends.
- The page moves it into memory and clears it from the address bar before
  connecting.

### Rendering

**Rendering.** `term.js` is a terminal model written for what croft emits,
not a general emulator. It covers:

- cursor addressing;
- SGR colour (16, 256, truecolor, and the colon forms) and attributes;
- erase and insert/delete;
- scroll regions;
- the alternate screen;
- the private modes croft sets;
- kitty keyboard flags (push, pop and the SET form croft re-asserts on every
  resize);
- wide and combining characters.

`app.js` paints its grid on a canvas.

- **Icons.**
  - `icons.woff2` holds the Nerd Font glyphs croft's source uses and nothing
    else (150 glyphs, 21 KB).
  - `scripts/web_icon_font.py` rebuilds it from the Meslo Nerd Font.
  - `scripts/tests/test_web_page.py` fails while an icon in `src/` is missing
    from it.
- **Images.**
  - Kitty (`APC G`, chunked PNG, placements replaced by id, `a=d` deletes) and
    iTerm2 (`OSC 1337;File=`) images become `<img>` elements over the cells
    they cover.
  - They are removed when those cells are written or the screen is cleared.
  - Sixel is not decoded.
- **Queries.**
  - Answered: DA1, cursor position, kitty flags, window and cell size, and
    default colours, so croft's probes get a reply.
  - Only the write-control holder's replies reach croft, as for any client.

### Page input

**Input.**

- Keys:
  - With kitty flags on, a key with Ctrl, Alt or Cmd is sent as
    `CSI code;mods u`, so `Cmd+K` reaches croft as `CSI 107;9u` exactly as
    from iTerm2 or Ghostty.
  - Without them, the legacy encodings.
  - Plain text goes through a hidden textarea, so IME composition, dead keys
    and phone keyboards send what they show.
  - The chords a browser keeps for itself are listed under [Input]#input;
    `Cmd+K` and the palette reach everything they would.
- Mouse: SGR reports for the modes croft enables, including drags (pane
  seams resize) and the wheel. Touch scrolling becomes wheel reports.
- Paste: bracketed when croft asked for it.
- Clipboard: croft's OSC 52 copies go to the system clipboard in a secure
  context (HTTPS, or `localhost`). Elsewhere the page says the copy was not
  made.

### Size

**Size.** The page sizes the grid to the window and sends `resize`. The
host's smallest-window rule applies: a small browser window shrinks the
session for everyone attached, as a small terminal would.

## Threat model

What croft web defends against:

- **Other local users.**
  - The loopback port is open to every account on the machine.
  - The token (128 bits from the system RNG, printed once) is the credential.
  - The session socket behind it stays 0600.
- **Other web pages.**
  - WebSockets are not bound by CORS, so any open page can try to connect.
  - A request with an `Origin` must match the scheme and `Host` it arrived on.
  - On a loopback listener the `Host` must also be a loopback name. This
    closes DNS rebinding: a hostile name resolved to 127.0.0.1 agrees with its
    own `Host`, but not with loopback.
  - The page also lacks the token.
- **Token leakage.**
  - The token is not in any URL, so it stays out of history, `Referer` and
    proxy logs.
  - It is not in the listener record.
  - It is compared in constant time.
- **The network, with `--tls`.** Everything, the token included, is inside
  TLS.

What it does not do, deliberately:

- **No accounts or per-user tokens.** Anyone with the token is one more
  participant. Write control is still the host's, per participant: a new
  connection is read-only unless it is the first or is granted control in
  `Session: Participants`.
- **No certificate generation or ACME.** Bring a certificate, or front the
  listener with Tailscale or Caddy.
- **No protection on a routable `--bind` without `--tls`.** croft serves it,
  and warns that anyone on the path sees the token and the screen.
- **No rate limiting.** 128 bits of token make guessing pointless. A flood of
  connections is a denial of service croft does not try to stop.
- **Nothing against the local account itself.** Whoever can read the
  terminal croft web was started in has the token, just as whoever owns the
  account already owns the session socket.