Files
ghostty/src
Mitchell Hashimoto f5880782fe terminal: add stream continuation tracking for replay (#13544)
This adds opt-in continuation tracking to `terminal.Stream` that allows
any caller to call `writeContinuation` in order to get the minimum bytes
necessary from a grounded parser state to the identical state.

This enables reliable stream restart across serialization states, which
could be used for local restart, networked terminals, etc. For me, this
is used for multiplexers. :)

**LLM usage:** I wrote the continuation tracker myself, used a mix of
5.6+Fable to review it for me, applied their feedback directly. Only
place with predominantly AI code are tests, which I reviewed. Commit and
PR message written myself.

## Implementation

The implementation of this was really carefully done to avoid any
negative performance impact particularly when continuation tracking is
_off_.

The way this work is simple:

1. ESC is the only char that leaves the ground state and most ESC
sequences are short. So if we're in a non-ground state, we do a
backwards vectorized search to find the last `ESC` in the input slice.
If one doesn't exist, we assume we found it previously and store the
whole slice (rare, since ESC sequences are usually short like I said).

2. If we're in the ground state that means we only have a potential
incomplete UTF-8 codepoint, so we find the lead UTF-8 byte.

3. When writing, we normalize the suffix to drop things like BEL
commands that would've already been handled to avoid double-calling.

## Performance

No real impact.

Via `ghostty-bench +terminal-stream`.

Corpus | Main | PR w/ Tracking Off | PR w/ Tracking On
-- | -- | -- | --
Plain ASCII (256 MiB) | 175.4 ms | 175.8 ms | 175.5 ms
UTF-8 (32 MiB) | 268.4 ms | 270.0 ms | 269.9 ms
5% invalid UTF-8 (32 MiB) | 316.4 ms | 317.8 ms | 320.2 ms
CSI-heavy (32 MiB) | 145.0 ms | 146.3 ms | 145.9 ms
OSC (32 MiB) | 1621.0 ms | 1627.5 ms | 1638.3 ms
Kitty APC (128 MiB) | 95.5 ms | 96.9 ms | 96.3 ms
Mixed traffic (32 MiB) | 172.4 ms | 172.0 ms | 172.3 ms
Giant APC (128 MiB) | 38.3 ms | 38.3 ms | 40.8 ms
2026-08-01 14:16:20 -07:00
..
2026-07-28 15:52:01 -05:00
2026-07-21 12:35:05 -07:00
2026-03-15 20:58:43 -04:00
2026-07-21 12:35:05 -07:00
2026-07-30 08:51:38 -07:00
2026-07-21 17:19:16 -07:00
2025-10-03 18:52:26 +02:00
2026-07-21 17:19:16 -07:00
2026-07-21 12:35:05 -07:00
2026-07-21 17:19:16 -07:00
2026-07-21 12:35:05 -07:00
2025-11-27 13:37:53 -08:00
2025-11-27 13:37:53 -08:00
2026-07-21 12:35:05 -07:00
2026-07-26 15:38:19 -07:00
2026-07-21 17:19:16 -07:00
2026-07-21 12:35:05 -07:00
2026-07-21 17:19:16 -07:00
2026-07-21 17:19:16 -07:00
2024-08-16 14:35:10 -07:00
2026-07-21 17:19:16 -07:00
2022-08-18 11:42:32 -07:00
2026-03-20 10:37:42 -05:00
2026-07-21 12:35:05 -07:00
2026-07-28 11:04:13 -05:00
2026-07-21 12:35:05 -07:00