# Updating Nerve Nerve ships a built-in updater. One command pulls the latest published release from GitHub, rebuilds, restarts the service, and checks that the new version is serving. If the update, build, service restart or health check fails after the snapshot, it tries to roll back to the previous version. When a newer release exists, the status bar shows an **Update** button (checked hourly). It opens a dialog with the command below for your install directory. ## Prerequisites - The install is a git checkout whose `origin` is an HTTPS URL (the installer creates one). - `git`, Node.js 22 or later, and `npm` are on `PATH`. - You have write permission in the install directory. - The updater is built. `npm run update` runs `bin-dist/bin/nerve-update.js`, which `npm run build` creates. Check the remote, and fix it if it is not HTTPS: ```bash git remote -v git remote set-url origin https://github.com//.git ``` ## Quick start ```bash cd ~/nerve npm run update -- --yes ``` On Linux the installer sets Nerve up as a system service (`/etc/systemd/system/nerve.service`), and the updater restarts it without `sudo`. If your user may not restart system services, update without the restart and restart the service yourself once the update succeeds: ```bash npm run update -- --yes --no-restart && sudo systemctl restart nerve.service ``` See [The service restart command fails](#the-service-restart-command-fails-exit-50-or-70) for what happens otherwise. The updater: 1. Takes the update lock. 2. Runs preflight checks: `git`, `node` and `npm` exist, Node is 22 or later, the directory is a git repository, `origin` is HTTPS, and the directory is writable. 3. Resolves the target version: the latest published GitHub release, or the latest semver tag if the release lookup fails. `--version` pins a version instead. 4. Asks for confirmation (skipped with `--yes`). 5. Snapshots the current state for rollback. 6. Runs `git fetch --tags origin`, then `git checkout --force `, which leaves the checkout on a detached HEAD at the tag. 7. Runs `npm install`, `npm run build` and `npm run build:server`. 8. Restarts the systemd or launchd service. 9. Polls `/health` and `/api/version` until the new version is serving. `git checkout --force` runs without a dirty check, so uncommitted edits to tracked files are discarded. Your `.env` is untracked and is never modified. If the resolved version equals the version in `package.json`, the updater exits with "Already up to date" (exit code 1). Pinning an older version with `--version` is allowed and acts as a downgrade. ## Command line | Flag | Description | |------|-------------| | `--version ` | Pin to a specific version instead of the latest. The leading `v` is optional, and the tag must exist on `origin`. | | `--yes`, `-y` | Skip the confirmation prompt. Use it when nobody can answer a prompt. | | `--dry-run` | Resolve the target version and print what would happen, without changing anything. | | `--verbose`, `-v` | Extra logging (git commands, service detection, health URLs). | | `--rollback` | Restore the last snapshot. | | `--no-restart` | Skip the service restart and the health checks. | | `--help`, `-h` | Show help. | `--rollback` ignores `--dry-run`, `--no-restart` and `--yes`. It does not ask for confirmation and restarts the service if it finds one. ```bash # Preview what an update would do npm run update -- --dry-run # Update to a specific version npm run update -- --version v1.4.0 --yes # Roll back to the state before the last update npm run update -- --rollback # Update without restarting (restart the service yourself) npm run update -- --yes --no-restart ``` ## Exit codes | Code | Meaning | |------|---------| | 0 | Success. Also returned for a dry run, a declined confirmation, and a finished `--rollback`. | | 1 | Already up to date. Also returned for an unknown option, a missing `--version` value, and an uncaught fatal error. | | 10 | Preflight failure: `git`, `node` or `npm` missing, Node below 22, not a git repository, `origin` not HTTPS, or no write permission. | | 20 | Version resolution failure: `package.json` unreadable, no release or semver tags found, or `--version` invalid or not an existing tag. | | 40 | Update or build failure: `git fetch`, `git checkout`, `npm install` or a build step failed (rollback attempted). Also returned for an unexpected error outside the tracked stages (no rollback). | | 50 | The service restart command failed, or the service was not active after the restart (rollback attempted). | | 60 | Health check failure: `/health` never returned 2xx, or `/api/version` never reported the target version (rollback attempted). | | 70 | Rollback failure (needs manual intervention), or `--rollback` found no snapshot. | | 80 | Lock acquisition failure: another update is running. | When a rollback succeeds, the exit code is still the code of the failure that triggered it. ## How it works ### Snapshots Before changing anything, the updater writes `~/.nerve/updater/last-good.json` with: - the current git commit hash, - the current version from `package.json`, - a timestamp, - a SHA-256 hash of `.env`. If `.env` exists, a copy also goes to `~/.nerve/updater/snapshots//.env` (mode `0600`). Each update overwrites `last-good.json`, so it always describes the state just before the most recent update attempt. ### Rollback Rollback restores the snapshot commit, cleans `node_modules`, rebuilds, and restarts the service. It runs automatically when the git update, a build step, the service restart command, the service-active check after the restart, or the health check fails, or manually with `--rollback`. 1. `git checkout --force ` (local, no fetch). 2. Remove `node_modules`. 3. `npm install && npm run build && npm run build:server`. 4. Restart the service and check that it is active. Rollback does not re-run the `/health` and `/api/version` checks. ### Health checks After the restart, the updater polls for up to 60 seconds: - `GET /health` must return 2xx. - `GET /api/version` must report the target version. Each request times out after 5 seconds. Between attempts it waits 2, 4, then 8 seconds (8 seconds from then on). A version mismatch keeps retrying, because an old process may still be serving. If the deadline passes with the wrong version, the check fails and triggers a rollback. The updater takes the host and port from its own environment first, then from `.env`, then the defaults (`127.0.0.1`, `3080`). A wildcard host (`0.0.0.0`, `::`) is probed on loopback. ### Locking A PID-based lock file prevents concurrent updates. It is created with the `wx` flag (exclusive create) and removed on exit. If the PID in an existing lock is no longer running, the lock is treated as stale and replaced. `--rollback` takes the same lock. ### Service detection The updater picks the first service manager that finds a match, checking systemd before launchd: | Manager | Detection | Restart | Active check | |---------|-----------|---------|--------------| | systemd | The first line of `systemctl list-units --type=service --all` that mentions "nerve" (case-insensitive), whose first column is the unit. System units are searched before `--user` units. | `systemctl [--user] restart ` | `systemctl [--user] is-active ` returns `active` | | launchd | macOS only. The first `launchctl list` entry containing "nerve" (for example `com.nerve.server`) | `launchctl kickstart -k gui//