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

+71
View File
@@ -0,0 +1,71 @@
/**
* Tiny ustar writer for the update tests — the real artifacts come from
* `tar -czf` in `scripts/build-binary.sh`, and `src/core/tarball.ts` has to read
* that shape without a `tar` subprocess.
*/
import { gzipSync } from "node:zlib";
export interface FixtureEntry {
name: string;
data?: string;
mode?: number;
type?: "file" | "directory" | "symlink";
}
const BLOCK = 512;
function octal(value: number, length: number): string {
const text = value.toString(8);
return "0".repeat(Math.max(0, length - 1 - text.length)) + text + "\0";
}
function header(entry: FixtureEntry): Buffer {
const buffer = Buffer.alloc(BLOCK);
const typeflag = entry.type === "directory" ? "5" : entry.type === "symlink" ? "2" : "0";
const data = Buffer.from(entry.data ?? "", "utf8");
buffer.write(entry.name, 0, 100, "utf8");
buffer.write(octal(entry.mode ?? (entry.type === "directory" ? 0o755 : 0o644), 8), 100, 8, "ascii");
buffer.write(octal(0, 8), 108, 8, "ascii");
buffer.write(octal(0, 8), 116, 8, "ascii");
buffer.write(octal(entry.type === "directory" ? 0 : data.length, 12), 124, 12, "ascii");
buffer.write(octal(1_700_000_000, 12), 136, 12, "ascii");
buffer.write(" ", 148, 8, "ascii");
buffer.write(typeflag, 156, 1, "ascii");
buffer.write("ustar\0", 257, 6, "ascii");
buffer.write("00", 263, 2, "ascii");
let sum = 0;
for (const byte of buffer) sum += byte;
buffer.write(`${octal(sum, 7)} `, 148, 8, "ascii");
return buffer;
}
export function buildTar(entries: FixtureEntry[]): Buffer {
const blocks: Buffer[] = [];
for (const entry of entries) {
blocks.push(header(entry));
if (entry.type === "directory" || entry.type === "symlink") continue;
const data = Buffer.from(entry.data ?? "", "utf8");
const pad = (BLOCK - (data.length % BLOCK)) % BLOCK;
blocks.push(data, Buffer.alloc(pad));
}
blocks.push(Buffer.alloc(BLOCK * 2));
return Buffer.concat(blocks);
}
export function buildTarGz(entries: FixtureEntry[]): Buffer {
return gzipSync(buildTar(entries));
}
/** The two trees `scripts/build-binary.sh` ships in a release asset. */
export function buildReleaseArchive(version = "0.1.7"): Buffer {
return buildTarGz([
{ name: "bin/", type: "directory" },
{ name: "bin/pi2", data: `#!/bin/sh\necho "pi2 ${version}"\n`, mode: 0o755 },
{ name: "share/", type: "directory" },
{ name: "share/pi2d/", type: "directory" },
{ name: "share/pi2d/web/", type: "directory" },
{ name: "share/pi2d/web/dist/", type: "directory" },
{ name: "share/pi2d/web/dist/index.html", data: "<html></html>\n" },
{ name: "LICENSE", data: "MIT\n" },
]);
}