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

+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.