Wink Pings

Terminal Restarts, Program Keeps Running: Rex Releases OSC 7577

Rex has released OSC 7577, a pure terminal escape sequence protocol. Programs hand their restore command to the terminal for safekeeping; after a restart, the terminal runs it for you. Claude Code already has built-in support, and other programs can use it by adding a few lines of code, with no dependency on any particular terminal.

Rex has released a feature: programs can resume running after the terminal restarts. It does not restore window layouts and scrollback; it reconnects running processes.

The implementation is via a terminal protocol also announced today, OSC 7577. It is pure escape sequences, does not depend on a specific terminal, does not require an out-of-band channel, and any program can support it by writing a few lines of code.

The problem itself is not new. When a terminal crashes, updates, or restarts, all the programs inside it disappear with it. Some people use tmux or screen to separate process and terminal lifecycles, but tmux itself also goes down on restart. Other tools match by process name and rerun commands with the same name after restart. The flaw in these approaches is that the startup command they get is often not the restore command.

For example. You open an editor and load a particular project, a particular session file, some history. When you started it, you may have typed only nvim. Process-name matching can only see nvim; it does not know which session you had opened. So it either restores to a blank state or simply does not restore at all.

OSC 7577 takes a different approach: let programs say for themselves how to bring themselves back.

A program writes an ESC ] 7577 ; ... sequence to the terminal, carrying its own pid, the current time, the base64-encoded argv, and a MAC. After the terminal verifies it, it saves this command as a record. On the next terminal startup, it runs the command from the record.

The whole thing is plain text. There is no other channel in the terminal; all communication is escape sequences, so the protocol can only be escape sequences too. This limits expressiveness, but it also makes it work for any program: no SDK, no binding to Rex, and no intermediary layer between the terminal and the program.

In the tweet, Mitchell Hashimoto says Rex has built-in support for Claude Code as the first program, sending OSC 7577 through a session start hook. Other programs can integrate following the same approach.

The security part deserves separate discussion.

Program restoration is inherently unsafe. A saved command will be executed without the user's knowledge. The protocol authors spent a lot of space addressing this. The core mechanism is a token: when the terminal starts its first process, it sets an environment variable OSC7577_TOKEN with a random value of 22 to 64 characters, different for every terminal and every launch. The program uses this token to compute a MAC for the report; the terminal recomputes it upon receipt and ignores it if they do not match.

This way, the output from cat-ing a file, tailing a log, or SSH-ing to a remote machine and printing something cannot forge a restore command. They do not know the token.

One interesting detail: the MAC uses ordinary SHA-256 with the token appended to the end of the string, not HMAC. The documentation explains it plainly: HMAC requires extra tools, and the common way to compute HMAC in a shell puts the token on the command line, where other users on the same machine can see it. Ordinary SHA-256 only needs sha256sum or shasum, with the token passed in via standard input. This tradeoff is very pragmatic, and it also shows that the protocol really considered how shell scripts would be written.

In addition, the time key limits a 60-second window, preventing someone from recording terminal output and replaying it. The recorded content contains no token, so a replay can only replay one message as-is, and it must happen within the time window.

The threat model is also written clearly: other programs by the same user are trusted, and lower-privilege child processes are outside the protection scope. Therefore, the documentation requires that programs which report themselves should remove the token from every child process they start; programs that do not report themselves should also remove the token if they start restricted child processes. Otherwise, something running in a sandbox could register a command and, after the terminal restarts, execute it with the user's full privileges.

There are limits too: the entire sequence must support at least 2048 bytes, and argv at least 64 elements. If exceeded, clear must be sent first to wipe the old record; otherwise the old command will be used for restoration.

Rex's configuration is written in Lua and can decide which programs are allowed to restore based on program name, path, working directory, and command-line arguments; one line can disable them all.

The protocol documentation also lists how it differs from several existing approaches. OSC 133 and OSC 633 can only describe what command the user typed at the prompt; they cannot say whether this command is safe or can be restored. OSC 88 is earlier work on the same idea; its reports are unauthenticated, and the terminal has to inspect the process to decide whether to trust them. Tools such as tmux-resurrect and kitty sessions read the command line of a running process and get the startup command, which may even contain an rm. Zellij, Herdr, and cmux each support programs reporting a restore command, but each program must first know which terminal it is running in and then call the corresponding tool; moving to another terminal means doing it all over again.

The core pain point OSC 7577 solves is handing the decision of "how to restore" to the program itself. Process-name matching cannot guess accurately, hardcoded lists are costly to maintain, and the program itself knows where its state is stored.

Whether it can become widespread is another matter. Terminal emulators have to be willing to implement it, and programs have to be willing to send reports; both sides need to move. But at least now, every step of this path is written down clearly. The protocol documentation even provides a table of invalid reports, making it clear at a glance which cases are ignored and which are rejected.

发布时间: 2026-11-26 14:30