# Restarting the Daemon

URL: https://tuios.dev/docs/daemon-restart

> Move the daemon to a new tuios build with tuios restart-server or from the app. tuios shows what stops first, and every attached client comes back by itself.

The daemon runs the tuios build it started with until it stops. After you update tuios, restart the daemon to move your sessions to the new build. `tuios restart-server` (Added in v0.11.0) does that in one step. First it shows what stops and what comes back, and asks you. Then it stops the daemon and starts the newest tuios you have installed. It waits until the new daemon answers.

![The Restart the daemon dialog over three panes. It shows the daemon's version now and after the restart, the programs that stop (sleep in api and python3 in server), and that each session comes back with its layout, its pane history and new shells.](https://tuios.dev/shots/restart-dialog.webp)

*The dialog the app shows. It is the same plan that tuios restart-server prints.*

## Quickstart

```bash
tuios update            # or your package manager
tuios restart-server    # show what stops, ask, then restart
```

From a shell, the command prints the plan and asks:

```
Daemon now      0.10.1
After restart   0.11.0   (from ~/.local/bin/tuios)

These stop:
  nvim in "edit" (session work)
  claude in "agent" (session work)
Only shells in notes.

These come back:
  Each session, with its layout, its pane history and new shells.
  claude-code in "agent" (session work) can resume.
The Inbox offers to resume each one.
Restart the daemon? [y/N]
```

Then it shows each step:

```
Stopping the daemon...
Starting the daemon 0.11.0...
The daemon restarted on 0.11.0.
These sessions came back: work, notes.
```

## What stops and what comes back

A restart ends every program that runs in a pane: editors, servers, agents, long builds. The plan names each one, by pane and session, so you can finish or save work first.

Each session comes back with its layout, the pane names and the history each pane showed. Every pane gets a new shell, under a divider that says when it was restored. An agent conversation that can resume is listed in the plan. `daemon.resume_agents` says what the new daemon does with it:

| Value  | The new daemon                                                    |
| ------ | ----------------------------------------------------------------- |
| `ask`  | Puts a Resume item in the Inbox for each one. This is the default |
| `auto` | Resumes each conversation in its pane                             |
| `off`  | Leaves them. `tuios resume-agent` resumes one later               |

See [Resuming after a restart](https://tuios.dev/docs/agent-inbox#resuming-after-a-restart).

![tuios restart-server --dry-run in the shell pane. It prints the daemon's version, the binary the restart runs, and that the sleep in api, python3 in server and the tuios command itself stop. Each session comes back with its layout, its pane history and new shells.](https://tuios.dev/shots/restart-plan.webp)

*--dry-run prints the plan and changes nothing.*

## Which tuios starts

The new daemon runs the newer of two binaries, by `--version`:

- The tuios at the path of the command you ran. `tuios update` replaces this file.
- The `tuios` on your `PATH`. An install by Nix, mise, asdf or Linuxbrew moves this one.

When the version does not change, the plan says "This restart starts the same version." The restart still saves and restores each session, so it is also a clean way to apply a change to `[daemon]` or `[hooks]`.

## From the app (Added in v0.11.0)

When the daemon runs a version other than your client, the dock says so, for example "The daemon runs 0.10.1. ctrl+b j restarts it." Click the notice, or press the key it names. Esc puts the notice off until the next start. These open the same dialog:

- The command palette entry "Restart the daemon".
- The row "Restart the daemon" at the top of the Daemon tab in the settings.
- The rail's menu for this machine.

The dialog shows the plan. Press r to restart, or Esc to cancel. The dialog then shows each step: stop the daemon, start the new one, attach again. tuios starts again on the new build, in the same session, and says "The daemon restarted on 0.11.0. Your session is back."

![The same three panes after the restart. Each pane has a restore divider and a new shell. The dock says: The daemon restarted on 0.11.0. Your session is back.](https://tuios.dev/shots/restart-back.webp)

*After r: the same session on the new daemon, with the pane history and new shells.*

A program can start or stop after you read the plan. Then the restart does not run, and the dialog asks you to read the new plan. If your session did not come back, tuios does not attach to it. The dialog says so and points to `tuios resurrect`.

The action is `restart_daemon`. It has no default key. To bind one:

```toml
[keybindings.prefix_mode]
restart_daemon = ["alt+r"]
```

The action is only on a client on the daemon's own machine. Over `tuios ssh` or tuios-web, run `tuios restart-server` on the daemon's machine.

## Other clients come back by themselves

Before it stops, the daemon tells each attached client why:

| The daemon stopped with                               | The client                                                                                                                       |
| ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `restart-server`, the app, or `kill-server --restart` | Waits up to 15 seconds for the new daemon, then attaches to its session again and says "The daemon restarted. tuios reattached." |
| A plain `kill-server`                                 | Quits at once                                                                                                                    |
| Nothing: the daemon crashed                           | Waits up to 6 seconds for a new daemon                                                                                           |

While a client waits, the dock says "Waiting for the daemon. Keys are not sent." Keys you type then are dropped. The panes are new after a restart, so a held key could land in a different shell. Sometimes no daemon comes back, or the new daemon does not hold the session. Then the client quits with "The connection to the TUIOS daemon was lost."

Only a client on the same machine waits and reattaches. A client of a daemon older than v0.11.0 gets no reason, so it waits 6 seconds and then quits. Attach again after that.

> **Other machines**
>
> `tuios hosts sync --restart` installs your version on the machines in `[hosts]` and restarts each daemon the same way, so the clients attached there reattach too. See [hosts sync](https://tuios.dev/docs/cli-reference#hosts-sync).

## From a script

```bash
tuios restart-server --dry-run          # print the plan, change nothing
tuios restart-server --yes              # restart without a question
tuios restart-server --yes --json       # one JSON object per step
tuios restart-server --last             # the steps of the last restart
```

- Without a terminal to ask on, the command refuses unless you give `--yes`. `--json` needs `--yes`.
- `--json` writes one object per line: `plan`, then `stopping`, `starting` and `done`. `done` holds `restarted`, `after`, `back` and `missing`. A failed step sets `error` and `fix`, and the command exits `1`.
- `--expect DIGEST` refuses the restart unless the plan has that digest. The app passes the digest of the plan you agreed to.
- When no daemon runs, the command starts one.

The stop and the start run in a process of their own, in a session of its own. So a restart still finishes when you start it from a pane of the daemon it stops. Each step goes to `restart.log` beside the daemon log. One restart runs at a time. A second one refuses with "Another restart of the daemon is running."

| Error                                               | What to do                                                                          |
| --------------------------------------------------- | ----------------------------------------------------------------------------------- |
| "The daemon did not stop. It still runs version X." | Run `tuios kill-server` to see why.                                                 |
| "The daemon did not start."                         | Run `tuios start-server` to see why. The sessions come back when the daemon starts. |
| "The daemon started and did not answer."            | Run `tuios ls` to check it, or `tuios logs`.                                        |
| "Another restart of the daemon is running."         | Wait for it to end. `tuios restart-server --last` shows its steps.                  |
| "The restart stopped before it finished."           | Run `tuios restart-server --last`, then `tuios ls`.                                 |

## Upgrade in place (Unreleased)

`tuios daemon upgrade` moves the running daemon to another tuios binary and ends no program. Every program in a pane keeps its pid, its terminal and its screen. The daemon keeps its pid too. Live upgrades are experimental, and only on macOS and Linux.

```bash
tuios daemon upgrade                             # the tuios that runs the command
tuios daemon upgrade --binary ~/bin/tuios-next   # another binary
tuios daemon upgrade --json                      # one JSON object with the result
```

```
Checking /usr/local/bin/tuios (v0.10.1) accepts this daemon's state (handoff v3)... ok
Paused 4 panes, saved their screens (34.3 KB).
Starting v0.10.1 in place (pid 8615 kept)...
Upgraded in 0.06 s. 4 panes and their programs kept.
```

- tuios first runs the new binary once to check that it can take the panes. When the check fails, nothing changes.
- tuios refuses a binary that neither you nor root owns, and a binary that its group or other users can write.
- The daemon runs the same file that it checked. When a folder on the path is renamed after the check, no other file takes its place. On Linux the daemon runs the open file. On macOS and other systems it runs a private copy of the file in its state folder. On macOS the copy keeps the quarantine mark of a downloaded binary.
- A script, such as a wrapper, still runs by its path. On macOS, a binary that you can run but not read also runs by its path. Keep these in folders that no other user can write.
- Each pane keeps its screen, the alternate screen, its modes and titles, and the last 1000 rows of its history. Agent state, mail and the event stream carry over.
- Attached clients are dropped, and a tuios client on this machine attaches again by itself. It waits up to 15 seconds for the new daemon, draws the session as it was, and says "The daemon was upgraded. tuios reattached." Keys typed while it waits are not sent.
- When the new binary is a newer build than the client's, the client runs it too (Unreleased): it starts the new binary as `tuios attach <session>` in the same terminal, with the flags you started it with, and says "The daemon was upgraded to \<version>. This window runs it too." An upgrade to an older release keeps the newer client. Set `client_follows_upgrade = false` in `[daemon]` to keep the old client.
- A client whose build cannot speak to the new daemon, a client attached to another machine's session, and the clients of `tuios ssh` and `tuios-web` exit instead. They say that your programs still run, and tell you to run `tuios attach`.
- When the new binary cannot take the panes, the daemon goes back to the old binary, and every program still runs. The command then exits `1`. Run `tuios logs` to see why.

From a pane, the upgrade waits for you. Each attached client shows a dialog with the binary's path and version, and the pane that asked. No is the default. On Windows, and for a daemon that runs inside `tuios-web` or the SSH server, use `tuios restart-server`. See [daemon upgrade](https://tuios.dev/docs/cli-reference#daemon-upgrade) for every refusal.

## How do I

- **Move to a new build after `tuios update`?** Run `tuios restart-server`, or click the version notice in the app.
- **See what a restart would stop, without a restart?** Run `tuios restart-server --dry-run`.
- **Restart from a deploy script?** Run `tuios restart-server --yes --json` and read the `done` line.
- **Stop the daemon and keep the clients from waiting?** Run `tuios kill-server`. The clients quit at once.
- **Start with no sessions at all?** Run `tuios kill-server`, then `tuios daemon --no-restore`.
- **Move to a new build and keep every program running?** Run `tuios daemon upgrade`. It is experimental.

## Related

*[An interactive figure goes here. Open the page to use it.](https://tuios.dev/docs/daemon-restart)*
