feat(cli): add pi2 update with a versioned install layout

The standalone (curl installer) channel had no upgrade path of its own: the
documented one was 're-run the installer', which the Neovim plugin cannot do
safely and which re-downloads 35 MB without version arithmetic, provenance or
rollback. ADR 0003 records the design; this is the implementation.

- install.sh now installs one immutable tree per version
  (`<prefix>/versions/<v>/{bin,share,LICENSE}`) behind a single atomically
  swapped `current` symlink, writes `<prefix>/install.json` as the install
  receipt, and migrates a pre-0.1.7 flat layout in place. Keeping `current` plus
  one previous tree gives a one-step rollback for free.
- `pi2 update` (native TypeScript, no `curl | sh` subprocess) resolves the
  release with an ETag-cached GitHub lookup, picks the asset from the
  build-time `PI2_BUILD_TARGET` define instead of re-probing libc, verifies
  SHA256SUMS, verifies SLSA provenance with `gh attestation verify`, runs the
  new binary once, then swaps `current` and rewrites the receipt. Flags:
  --check/--dry-run/--version/--force/--json/--require-attestation/
  --skip-attestation. Downgrades need --force; restarts stay manual.
- the receipt is the only channel detection: npm, Homebrew and the AUR are
  refused with the command that owns their tree, and a flat install is told to
  re-run the installer once. No heuristics on `process.execPath`.
- `pi2 install-service` renders `<prefix>/current/bin/pi2`, so an update no
  longer invalidates the unit.
- `scripts/build-binary.sh` injects `PI2_BUILD_TARGET`; `tarball.ts` is a
  dependency-free ustar/PAX reader with traversal and symlink rejection.

Verified against the real release: install.sh into a scratch prefix, update
--check/--dry-run, a local 0.1.7 mirror upgrade (attestation warn path), then
`pi2 update --version 0.1.6 --force` (real download, real attestation
verified), plus boot-check and the refusal paths. typecheck + 567 daemon tests
+ web typecheck + 537 web tests + smoke are green.
This commit is contained in:
yuez committed 2026-10-29 16:42:57 +08:00
1 parent 29aee26733
commit 8fb535f7f3
20 files changed
+2557 -43

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
# ADR 0003 — `pi2 update`: a versioned install layout and an install receipt
- Status: accepted (implementing)
- Date: 2026-10-29
- Extends: [ADR 0002](0002-install-and-run.md) §2.9.1 (the deferred `pi2 update`)
## Context
ADR 0002 deferred an in-place self-updater because "package managers already upgrade
well". That holds for Homebrew, the AUR and npm, but not for the **standalone channel**
(Channel B, `scripts/install.sh`): the upstream upgrade path there is "re-run the
installer", which the Neovim plugin cannot do safely and which forces a re-download of
the whole 35 MB artifact with no version arithmetic, no provenance check and no
rollback.
Two facts shaped the design:
1. **A compiled `bun build --compile` binary cannot be written in place** (`ETXTBSY`),
but `rename()` over the running executable works on both Linux and macOS, and the
running process keeps its old inode. So replacement must be a staged rename, never
an in-place write.
2. **The install is two trees** (`bin/` + `share/pi2d/web/dist`), and renaming two
directories is not atomic. A half-finished update would leave a new binary with an
old UI.
`pi2` cannot guess its channel reliably from its own path: `~/.local/bin/pi2`,
`$(brew --prefix)/bin/pi2`, `/usr/bin/pi2` and `<data>/pi2d/node_modules/.bin/pi2` all
look equally like "some binary". Guessing wrong means either corrupting a
package-manager-owned tree or refusing a legitimate update.
## Decision
### 1. The curl channel installs into a versioned layout
```
<prefix>/ default ~/.local/lib/pi2d (PI2_INSTALL_DIR)
versions/
0.1.6/
bin/pi2
share/pi2d/web/dist/…
LICENSE
0.1.7/ ← one immutable tree per version
current -> versions/0.1.7 ← relative symlink, the only thing that moves
install.json ← the receipt (never inside a version tree)
<bin dir>/pi2 -> <prefix>/current/bin/pi2
```
A version tree is immutable once written, so `current` is the single atomic switch and
`bin/` + `share/` can never disagree. Rollback is one symlink write, and the previous
tree stays on disk (pruned to `current` + 1 previous). `PI2_BIN_DIR/pi2` points at
`current`, so the plugin, the user's `PATH` and the rendered service unit all keep
working across an update — **`ExecStart` must use the `current` path, not
`<prefix>/versions/<v>/bin/pi2`**, or the unit breaks on every update.
The tarball keeps its flat `bin/` + `share/` shape: the version directory is created by
the installer, not by the release artifact.
### 2. The installer writes a receipt, and only the receipt authorises an update
`<prefix>/install.json`:
```json
{
"schema": 1,
"installer": "curl",
"layout": "versioned",
"prefix": "/home/u/.local/lib/pi2d",
"current": "/home/u/.local/lib/pi2d/current",
"version": "0.1.6",
"bin": "/home/u/.local/lib/pi2d/versions/0.1.6/bin/pi2",
"asset": "pi2d-0.1.6-linux-x64.tar.gz",
"sha256": "d560f84c…",
"installed_at": "2026-10-29T07:32:00Z"
}
```
`pi2 update` refuses unless **all** of these hold:
| Condition | Failure message points at |
|---|---|
| running as a compiled binary | `npm i -g @yuez/pi2d@latest` (npm channel) |
| `<prefix>/install.json` exists | `brew upgrade pi2d` / an AUR helper / "re-run the installer once" (legacy flat layout) |
| `installer == "curl"` | the channel's own command (`brew upgrade`, `paru -Syu pi2d-bin`, `npm i -g`) |
| `bin == realpath(process.execPath)` | "another copy of pi2 is installed" |
`installer` is written by whoever installed it: `install.sh` writes `curl`; the
Homebrew formula and the AUR `PKGBUILD` write `brew` / `aur` when they can, and their
absence merely lands in the "no receipt" row above. npm installs never write one.
This is the `uv` model (`load_receipt()` + "assume a package manager when no receipt is
found"), which is the only channel detection that cannot be spoofed by a stray binary
on `PATH`.
### 3. `pi2 update` implements the transfer itself (native TypeScript)
Not "download and pipe the installer into `sh`" (uv's approach): the updater must be
hermetic-testable (`AGENTS.md` hard rule 5), must not execute remote code, and has to
verify provenance — none of which a `curl | sh` subprocess gives us. So:
- release + asset resolution via the GitHub API, cached by ETag in
`<state>/update-check.json` (unauthenticated API is 60 req/h; `pi2 update --check`
must not burn it),
- asset selection from the build-time `PI2_BUILD_TARGET` define, not runtime libc
probing,
- `SHA256SUMS` verification, then **SLSA provenance** via `gh attestation verify`
(required with `--require-attestation`; otherwise a warning, because `gh` is not
guaranteed to be installed),
- extraction with a dependency-free tar reader, so no `tar`/`sh` subprocess,
- the staged binary is executed (`--version`) before anything is swapped.
### 4. Defaults
| Behaviour | Default | Override |
|---|---|---|
| restarts a running service | no, prints the command | — (`pi2 service stop && pi2 service start`) |
| downgrade | refused | `--force` |
| same version | "already up to date", no download | `--force` |
| attestation | verify when `gh` is available, else warn | `--require-attestation`, `--skip-attestation` |
| mirror / offline test | GitHub releases | `PI2_BASE_URL` (same knob as `install.sh`) |
| version lookups | cached 24 h | `PI2_UPDATE_CACHE_TTL` |
Restart stays manual on purpose: the daemon is shared by every Neovim instance and the
browser, and silently bouncing it mid-task is worse than asking.
## Consequences
- `scripts/install.sh` gains the versioned layout, the receipt and a migration from the
flat layout; `pi2 install-service` must render the `current` path.
- The flat layout is dead for the curl channel. An existing flat install keeps working
but cannot self-update until the installer runs once more.
- `pi2 update` is curl-only by design. Everything else is told to use its own package
manager, which is what ADR 0002 wanted.
- A future single-file artifact (embedding `web/dist` into the binary) would make the
swap a single rename, but `import … with { type: "file" }` gives no directory
listing and no `realpath`, which the static-file guard in `src/daemon/static.ts`
relies on. Not worth it today.
- Windows is out of scope: the whole release matrix is Linux/macOS, and an
in-process on Windows self-replace needs a helper process.
+10 -6
View File
@@ -141,15 +141,19 @@ second daemon cannot invalidate the running one.
1. **npm / Node package**: executed via Node (`node …/dist/cli/pi2.js`), where assets sit
next to the source tree.
2. **Compiled single binary**: compiled via Bun (`bun build --compile`) and installed as
`<prefix>/bin/pi2` with assets at `<prefix>/share/pi2d/web/dist`.
`<prefix>/versions/<version>/bin/pi2` with assets in
`<prefix>/versions/<version>/share/pi2d/web/dist`; `<prefix>/current` points at the live
version (see [ADR 0003](adr/0003-self-update.md), `pi2 update`).
How `pi2` discovers its own version and web UI (`src/core/install.ts`):
- **Version**: build-time define (`PI2_BUILD_VERSION`, injected by `scripts/build-binary.sh`)
→ on-disk `package.json` (`../../package.json` next to `dist/cli/` for npm/dev checkout)
→ `0.0.0` (`UNKNOWN_VERSION`).
- **Static UI directory** (`defaultStaticDir()`): probed in order, first with `index.html` wins:
1. `<exe>/../share/pi2d/web/dist` — release layout: `<prefix>/bin/pi2` plus
`<prefix>/share/pi2d/web/dist` (what the curl installer, Homebrew tap, and AUR `-bin` package install)
1. `<exe>/../share/pi2d/web/dist` — release layout: `<prefix>/versions/<v>/bin/pi2` plus
`<prefix>/versions/<v>/share/pi2d/web/dist` (what the curl installer writes; Homebrew and
the AUR `-bin` package use the flat `<prefix>/bin/pi2` + `<prefix>/share/pi2d/web/dist`,
which resolves to the same relative path)
2. `<exe>/web/dist` — portable unpacked directory (`pi2` next to its UI)
3. `<repo>/web/dist` — npm install or dev checkout relative to module location
- **Environment override**: `PI2_STATIC_DIR` still wins over all candidate paths when set.
@@ -519,8 +523,8 @@ After=network.target
[Service]
Type=simple
# For a compiled binary install:
ExecStart=/home/user/.local/lib/pi2d/bin/pi2 daemon --foreground
# For a compiled binary install (the `current` indirection survives `pi2 update`):
ExecStart=/home/user/.local/lib/pi2d/current/bin/pi2 daemon --foreground
# Or for an npm / Node install:
# ExecStart=/usr/bin/node /repo/dist/cli/pi2.js daemon --foreground
EnvironmentFile=-%h/.config/pi2/env
@@ -535,7 +539,7 @@ WantedBy=default.target
```
The `ExecStart` line adapts automatically to how `pi2` was invoked:
- **Compiled binary install**: `<prefix>/bin/pi2 daemon --foreground` (invokes the compiled executable directly).
- **Compiled binary install**: `<prefix>/current/bin/pi2 daemon --foreground` (invokes the compiled executable through the stable symlink, so an update does not break the unit; a flat Homebrew/AUR install renders its own binary path).
- **npm / Node install**: `<nodePath> <scriptPath> daemon --foreground` (e.g. `/usr/bin/node /repo/dist/cli/pi2.js daemon --foreground`).
The unit includes `EnvironmentFile=-%h/.config/pi2/env`, where the leading `-` tells systemd that a missing file is completely fine.
+37 -10
View File
@@ -13,7 +13,7 @@ Find your situation in the table below to choose the right installation command
| Situation | Recommended Command | What it gives you |
|---|---|---|
| **I have Node 20+** | `npm i -g @yuez/pi2d`<br>*(or `pnpm add -g`, `bun add -g`, `mise use -g npm:@yuez/pi2d`)* | The `pi2` CLI running directly on your existing Node.js environment, updated via standard package manager commands. |
| **no Node / old Node** | `curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh \| sh` | Standalone compiled binary in `~/.local/lib/pi2d` symlinked to `~/.local/bin/pi2`. Requires zero Node/npm dependencies and no root access. |
| **no Node / old Node** | `curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh \| sh` | Standalone compiled binary in `~/.local/lib/pi2d` symlinked to `~/.local/bin/pi2`, upgraded in place with `pi2 update`. Requires zero Node/npm dependencies and no root access. |
| **macOS** | `brew install zgs225/tap/pi2d`<br>*(or the curl installer)* | Native Homebrew formula. Places `pi2` in `$(brew --prefix)/bin` and web assets in `$(brew --prefix)/share/pi2d/web/dist`. |
| **Arch / EndeavourOS / Manjaro** | `curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh \| sh`<br>*(AUR `pi2d-bin` once AUR registration reopens)* | Standalone binary in `~/.local/lib/pi2d` + `~/.local/bin/pi2`. Package-manager channels are paused: the AUR is not accepting accounts, so the curl installer is the working path today. |
| **NixOS** | `curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh \| sh`<br>*(or dev checkout)* | Standalone binary in user home (`~/.local/bin/pi2`). Bun's `-musl` binaries are dynamically linked against the musl loader, so running a prebuilt binary on NixOS needs `nix-ld` (or an FHS env / `steam-run`-style wrapper). |
@@ -61,12 +61,20 @@ curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh
The script automatically detects OS (`linux`, `darwin`), architecture (`x64`, `arm64`), and C library (`glibc`, `musl`). It downloads the corresponding precompiled standalone archive from GitHub Releases, verifies its SHA256 checksum, and extracts it with zero root privileges needed.
**Where files land**:
- Base directory: `~/.local/lib/pi2d/`
- `~/.local/lib/pi2d/bin/pi2` (the compiled binary)
- `~/.local/lib/pi2d/share/pi2d/web/dist/` (the web console UI assets)
- `~/.local/lib/pi2d/LICENSE`
- Symlink: `~/.local/bin/pi2` points to `~/.local/lib/pi2d/bin/pi2`
**Where files land** (the versioned layout, see [ADR 0003](adr/0003-self-update.md)):
- Base directory (`<prefix>`): `~/.local/lib/pi2d/`
- `~/.local/lib/pi2d/versions/0.1.7/bin/pi2` (one immutable tree per version)
- `~/.local/lib/pi2d/versions/0.1.7/share/pi2d/web/dist/` (the web console UI assets)
- `~/.local/lib/pi2d/versions/0.1.7/LICENSE`
- `~/.local/lib/pi2d/current` → `versions/0.1.7` (the only thing an update moves)
- `~/.local/lib/pi2d/install.json` (the install receipt: installer, version, asset, SHA256)
- Symlink: `~/.local/bin/pi2` → `~/.local/lib/pi2d/current/bin/pi2`
Keeping one tree per version means `bin/` and `share/` can never disagree, an update is a
single symlink switch, and the previous version stays behind for one-step rollback (`pi2
update --version <old>`). The receipt is what authorises `pi2 update`: without one (npm,
Homebrew, AUR, or a flat install from before 0.1.7) it refuses and points at the command that
owns the tree.
Ensure `~/.local/bin` is in your `$PATH` (e.g. in your `~/.bashrc` or `~/.zshrc`):
```bash
@@ -141,7 +149,7 @@ pi2 install-service --write
```
The CLI inspects how it was installed:
- If running as a standalone compiled binary, `ExecStart` directly executes the binary (e.g. `/home/user/.local/lib/pi2d/bin/pi2 daemon --foreground`).
- If running as a standalone compiled binary, `ExecStart` directly executes the binary through the stable `current` symlink (e.g. `/home/user/.local/lib/pi2d/current/bin/pi2 daemon --foreground`), so `pi2 update` does not invalidate the unit.
- If running via Node, `ExecStart` points to `<node> <scriptPath> daemon --foreground`.
#### Linux (`systemd --user`)
@@ -313,10 +321,29 @@ systemctl --user restart pi2.service
```
- **Channel B (Standalone curl installer)**:
Re-run the installer. It replaces the binary and assets cleanly in `~/.local/lib/pi2d`:
```bash
curl -fsSL https://raw.githubusercontent.com/zgs225/pi2d/main/scripts/install.sh | sh
pi2 update
```
It downloads the release asset for this platform, checks it against the release's
`SHA256SUMS`, verifies its SLSA build provenance with `gh attestation verify` when the
GitHub CLI is installed, runs the new binary once, then switches
`~/.local/lib/pi2d/current` and rewrites the install receipt. The previous version is kept
for one-step rollback.
| Command | What it does |
|---|---|
| `pi2 update` | Upgrade to the latest release, then print the command to restart the daemon |
| `pi2 update --check` | Report the latest version and exit (24 h cached, one GitHub API call at most) |
| `pi2 update --dry-run` | Print the asset, URL and prefix it would use, change nothing |
| `pi2 update --version 0.1.6` | Pin a version (a downgrade also needs `--force`) |
| `pi2 update --force` | Reinstall even when already on that version |
| `pi2 update --require-attestation` | Fail unless the SLSA provenance verifies |
| `pi2 update --skip-attestation` | Never call `gh` (offline machines, no GitHub CLI) |
| `pi2 update --json` | Machine-readable result (stdout is pure JSON; progress goes to stderr) |
Setting `PI2_BASE_URL` points the download at a mirror (`PI2_UPDATE_CACHE_TTL` changes the
version-lookup cache, default 86400 s). Re-running the installer still works and stays the
recovery path when the tree is damaged.
- **Channel C (Homebrew)**:
```bash